Создание круга
Круг на карте можно отобразить на карте в виде YMapFeature. Для получения GeoJson в виде круга используется turf.circle.
vanilla.html
react.html
vue.html
common.css
common.ts
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1" />
<script crossorigin src="https://cdn.jsdelivr.net/npm/@babel/standalone@7/babel.min.js"></script>
<script crossorigin src="https://cdn.jsdelivr.net/npm/@turf/turf@7"></script>
<!-- To make the map appear, you must add your apikey -->
<script src="https://api-maps.yandex.ru/v3/?apikey=<YOUR_APIKEY>&lang=en_US" type="text/javascript"></script>
<script
data-plugins="transform-modules-umd"
data-presets="typescript"
type="text/babel"
src="./common.ts"
></script>
<script data-plugins="transform-modules-umd" data-presets="typescript" type="text/babel">
import {
CIRCLE_CENTER,
CIRCLE_DASHED_CENTER,
CIRCLE_DASHED_STYLE,
CIRCLE_RADIUS,
CIRCLE_STYLE,
CIRCLE_WITHOUT_STROKE_CENTER,
CIRCLE_WITHOUT_STROKE_STYLE,
LOCATION,
getCircleGeoJSON
} from './common';
window.map = null;
main();
async function main() {
// Waiting for all api elements to be loaded
await ymaps3.ready;
const {YMap, YMapDefaultSchemeLayer, YMapDefaultFeaturesLayer, YMapFeature} = ymaps3;
// Initialize the map
map = new YMap(
// Pass the link to the HTMLElement of the container
document.getElementById('app'),
// Pass the map initialization parameters
{location: LOCATION, showScaleInCopyrights: true},
// Add a map scheme layer
[new YMapDefaultSchemeLayer({}), new YMapDefaultFeaturesLayer({})]
);
// Create default circle
const circle = new YMapFeature({
geometry: getCircleGeoJSON(CIRCLE_CENTER, CIRCLE_RADIUS),
style: CIRCLE_STYLE
});
map.addChild(circle);
// Create circle with custom styles
const circleDashed = new YMapFeature({
geometry: getCircleGeoJSON(CIRCLE_DASHED_CENTER, CIRCLE_RADIUS),
style: CIRCLE_DASHED_STYLE
});
map.addChild(circleDashed);
// Create circle without stroke
const circleWithoutStroke = new YMapFeature({
geometry: getCircleGeoJSON(CIRCLE_WITHOUT_STROKE_CENTER, CIRCLE_RADIUS),
style: CIRCLE_WITHOUT_STROKE_STYLE
});
map.addChild(circleWithoutStroke);
}
</script>
<!-- prettier-ignore -->
<style> html, body, #app { width: 100%; height: 100%; margin: 0; padding: 0; font-family: Arial, Helvetica, sans-serif; } .toolbar { position: absolute; z-index: 1000; top: 0; left: 0; display: flex; align-items: center; padding: 16px; } .toolbar a { padding: 16px; } </style>
<link rel="stylesheet" href="./common.css" />
</head>
<body>
<div id="app"></div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1" />
<script crossorigin src="https://cdn.jsdelivr.net/npm/react@17/umd/react.production.min.js"></script>
<script crossorigin src="https://cdn.jsdelivr.net/npm/react-dom@17/umd/react-dom.production.min.js"></script>
<script crossorigin src="https://cdn.jsdelivr.net/npm/@babel/standalone@7/babel.min.js"></script>
<script crossorigin src="https://cdn.jsdelivr.net/npm/@turf/turf@7"></script>
<!-- To make the map appear, you must add your apikey -->
<script src="https://api-maps.yandex.ru/v3/?apikey=<YOUR_APIKEY>&lang=en_US" type="text/javascript"></script>
<script
data-plugins="transform-modules-umd"
data-presets="react, typescript"
type="text/babel"
src="./common.ts"
></script>
<script data-plugins="transform-modules-umd" data-presets="react, typescript" type="text/babel">
import {
CIRCLE_CENTER,
CIRCLE_DASHED_CENTER,
CIRCLE_DASHED_STYLE,
CIRCLE_RADIUS,
CIRCLE_STYLE,
CIRCLE_WITHOUT_STROKE_CENTER,
CIRCLE_WITHOUT_STROKE_STYLE,
LOCATION,
getCircleGeoJSON
} from './common';
window.map = null;
main();
async function main() {
// For each object in the JS API, there is a React counterpart
// To use the React version of the API, include the module @yandex/ymaps3-reactify
const [ymaps3React] = await Promise.all([ymaps3.import('@yandex/ymaps3-reactify'), ymaps3.ready]);
const reactify = ymaps3React.reactify.bindTo(React, ReactDOM);
const {YMap, YMapDefaultSchemeLayer, YMapDefaultFeaturesLayer, YMapFeature} = reactify.module(ymaps3);
const {useMemo} = React;
function App() {
const circleGeometry = useMemo(() => getCircleGeoJSON(CIRCLE_CENTER, CIRCLE_RADIUS), []);
const circleDashedGeometry = useMemo(() => getCircleGeoJSON(CIRCLE_DASHED_CENTER, CIRCLE_RADIUS), []);
const circleWithoutStrokeGeometry = useMemo(
() => getCircleGeoJSON(CIRCLE_WITHOUT_STROKE_CENTER, CIRCLE_RADIUS),
[]
);
return (
// Initialize the map and pass initialization parameters
<YMap location={LOCATION} showScaleInCopyrights={true} ref={(x) => (map = x)}>
{/* Add a map scheme layer */}
<YMapDefaultSchemeLayer />
<YMapDefaultFeaturesLayer />
<YMapFeature geometry={circleGeometry} style={CIRCLE_STYLE} />
<YMapFeature geometry={circleDashedGeometry} style={CIRCLE_DASHED_STYLE} />
<YMapFeature geometry={circleWithoutStrokeGeometry} style={CIRCLE_WITHOUT_STROKE_STYLE} />
</YMap>
);
}
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('app')
);
}
</script>
<!-- prettier-ignore -->
<style> html, body, #app { width: 100%; height: 100%; margin: 0; padding: 0; font-family: Arial, Helvetica, sans-serif; } .toolbar { position: absolute; z-index: 1000; top: 0; left: 0; display: flex; align-items: center; padding: 16px; } .toolbar a { padding: 16px; } </style>
<link rel="stylesheet" href="./common.css" />
</head>
<body>
<div id="app"></div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1" />
<script crossorigin src="https://cdn.jsdelivr.net/npm/vue@3/dist/vue.global.js"></script>
<script crossorigin src="https://cdn.jsdelivr.net/npm/@babel/standalone@7/babel.min.js"></script>
<script crossorigin src="https://cdn.jsdelivr.net/npm/@turf/turf@7"></script>
<script src="https://api-maps.yandex.ru/v3/?apikey=<YOUR_APIKEY>&lang=en_US" type="text/javascript"></script>
<script
data-plugins="transform-modules-umd"
data-presets="typescript"
type="text/babel"
src="./common.ts"
></script>
<script data-plugins="transform-modules-umd" data-presets="typescript" type="text/babel">
import {
CIRCLE_CENTER,
CIRCLE_DASHED_CENTER,
CIRCLE_DASHED_STYLE,
CIRCLE_RADIUS,
CIRCLE_STYLE,
CIRCLE_WITHOUT_STROKE_CENTER,
CIRCLE_WITHOUT_STROKE_STYLE,
LOCATION,
getCircleGeoJSON
} from './common';
window.map = null;
async function main() {
// For each object in the JS API, there is a Vue counterpart
// To use the Vue version of the API, include the module @yandex/ymaps3-vuefy
const [ymaps3Vue] = await Promise.all([ymaps3.import('@yandex/ymaps3-vuefy'), ymaps3.ready]);
const vuefy = ymaps3Vue.vuefy.bindTo(Vue);
const {YMap, YMapDefaultSchemeLayer, YMapDefaultFeaturesLayer, YMapFeature} = vuefy.module(ymaps3);
const app = Vue.createApp({
components: {YMap, YMapDefaultSchemeLayer, YMapDefaultFeaturesLayer, YMapFeature},
setup() {
const refMap = (ref) => {
window.map = ref?.entity;
};
const circleGeometry = Vue.ref(getCircleGeoJSON(CIRCLE_CENTER, CIRCLE_RADIUS));
const circleDashedGeometry = Vue.ref(getCircleGeoJSON(CIRCLE_DASHED_CENTER, CIRCLE_RADIUS));
const circleWithoutStrokeGeometry = Vue.ref(
getCircleGeoJSON(CIRCLE_WITHOUT_STROKE_CENTER, CIRCLE_RADIUS)
);
return {
LOCATION,
CIRCLE_CENTER,
CIRCLE_DASHED_CENTER,
CIRCLE_DASHED_STYLE,
CIRCLE_WITHOUT_STROKE_STYLE,
CIRCLE_WITHOUT_STROKE_CENTER,
CIRCLE_STYLE,
circleGeometry,
circleDashedGeometry,
circleWithoutStrokeGeometry,
refMap
};
},
template: `
<YMap :location="LOCATION" :showScaleInCopyrights="true" :ref="refMap">
<YMapDefaultSchemeLayer />
<YMapDefaultFeaturesLayer />
<YMapFeature :geometry="circleGeometry" :style="CIRCLE_STYLE" />
<YMapFeature :geometry="circleDashedGeometry" :style="CIRCLE_DASHED_STYLE" />
<YMapFeature :geometry="circleWithoutStrokeGeometry" :style="CIRCLE_WITHOUT_STROKE_STYLE" />
</YMap>`
});
app.mount('#app');
}
main();
</script>
<!-- prettier-ignore -->
<style> html, body, #app { width: 100%; height: 100%; margin: 0; padding: 0; font-family: Arial, Helvetica, sans-serif; } .toolbar { position: absolute; z-index: 1000; top: 0; left: 0; display: flex; align-items: center; padding: 16px; } .toolbar a { padding: 16px; } </style>
<link rel="stylesheet" href="./common.css" />
</head>
<body>
<div id="app"></div>
</body>
</html>
.test {
}
import type {DrawingStyle, LngLat, PolygonGeometry, YMapLocationRequest} from '@yandex/ymaps3-types';
declare global {
const turf: typeof import('@turf/turf');
}
export const LOCATION: YMapLocationRequest = {
center: [37.623082, 55.75254], // starting position [lng, lat]
zoom: 12 // starting zoom
};
export const CIRCLE_CENTER: LngLat = [37.618536, 55.760257];
export const CIRCLE_RADIUS = 1500;
export const CIRCLE_STYLE: DrawingStyle = {simplificationRate: 0};
export const CIRCLE_DASHED_CENTER: LngLat = [37.608301, 55.738633];
export const CIRCLE_DASHED_STYLE: DrawingStyle = {
simplificationRate: 0,
stroke: [{color: '#006efc', width: 4, dash: [5, 10]}],
fill: 'rgba(56, 56, 219, 0.5)'
};
export const CIRCLE_WITHOUT_STROKE_CENTER: LngLat = [37.660536, 55.730257];
export const CIRCLE_WITHOUT_STROKE_STYLE: DrawingStyle = {simplificationRate: 0, stroke: []};
export const getCircleGeoJSON = (center: LngLat, radiusMeters: number): PolygonGeometry => {
const {geometry} = turf.circle(center, radiusMeters, {units: 'meters'});
return geometry as PolygonGeometry;
};