在 google 地图中用圆圈拖动标记,反之亦然
Drag marker with circle and vice versa in google maps
是否可以"stick"标记到圆心?所以当我拖动标记圈时,圆圈也会移动,反之亦然。
options = {
strokeColor: "#0000FF",
strokeOpacity: 0.35,
strokeWeight: 2,
fillColor: "#0000FF",
fillOpacity: 0.20
};
options.map = context.map;
options.radius = radius;
options.center = center;
context.circle = new google.maps.Circle(options);
var marker = new google.maps.Marker({
position: new google.maps.LatLng(54.19335, -3.92695),
map: _map,
title: "",
draggable: True
});
将标记位置绑定到圆心属性。
// Add the circle for this city to the map.
var cityCircle = new google.maps.Circle(populationOptions);
var marker = new google.maps.Marker({
position: citymap[city].center,
draggable: true,
map: map
});
marker.bindTo("position", cityCircle, "center");
工作代码片段:
function initialize() {
// Create the map.
var mapOptions = {
zoom: 4,
center: new google.maps.LatLng(37.09024, -95.712891),
mapTypeId: google.maps.MapTypeId.TERRAIN
};
var map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
// Construct the circle for each value in citymap.
// Note: We scale the area of the circle based on the population.
for (var city in citymap) {
var populationOptions = {
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: '#FF0000',
fillOpacity: 0.35,
map: map,
draggable: true,
center: citymap[city].center,
radius: Math.sqrt(citymap[city].population) * 100
};
// Add the circle for this city to the map.
var cityCircle = new google.maps.Circle(populationOptions);
var marker = new google.maps.Marker({
position: citymap[city].center,
draggable: true,
map: map
});
marker.bindTo("position", cityCircle, "center");
}
}
google.maps.event.addDomListener(window, 'load', initialize);
// This example creates circles on the map, representing
// populations in North America.
// First, create an object containing LatLng and population for each city.
var citymap = {};
citymap['chicago'] = {
center: new google.maps.LatLng(41.878113, -87.629798),
population: 2714856
};
citymap['newyork'] = {
center: new google.maps.LatLng(40.714352, -74.005973),
population: 8405837
};
citymap['losangeles'] = {
center: new google.maps.LatLng(34.052234, -118.243684),
population: 3857799
};
citymap['vancouver'] = {
center: new google.maps.LatLng(49.25, -123.1),
population: 603502
};
var cityCircle;
html,
body,
#map-canvas {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk" ></script>
<div id="map-canvas"></div>
是否可以"stick"标记到圆心?所以当我拖动标记圈时,圆圈也会移动,反之亦然。
options = {
strokeColor: "#0000FF",
strokeOpacity: 0.35,
strokeWeight: 2,
fillColor: "#0000FF",
fillOpacity: 0.20
};
options.map = context.map;
options.radius = radius;
options.center = center;
context.circle = new google.maps.Circle(options);
var marker = new google.maps.Marker({
position: new google.maps.LatLng(54.19335, -3.92695),
map: _map,
title: "",
draggable: True
});
将标记位置绑定到圆心属性。
// Add the circle for this city to the map.
var cityCircle = new google.maps.Circle(populationOptions);
var marker = new google.maps.Marker({
position: citymap[city].center,
draggable: true,
map: map
});
marker.bindTo("position", cityCircle, "center");
工作代码片段:
function initialize() {
// Create the map.
var mapOptions = {
zoom: 4,
center: new google.maps.LatLng(37.09024, -95.712891),
mapTypeId: google.maps.MapTypeId.TERRAIN
};
var map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
// Construct the circle for each value in citymap.
// Note: We scale the area of the circle based on the population.
for (var city in citymap) {
var populationOptions = {
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: '#FF0000',
fillOpacity: 0.35,
map: map,
draggable: true,
center: citymap[city].center,
radius: Math.sqrt(citymap[city].population) * 100
};
// Add the circle for this city to the map.
var cityCircle = new google.maps.Circle(populationOptions);
var marker = new google.maps.Marker({
position: citymap[city].center,
draggable: true,
map: map
});
marker.bindTo("position", cityCircle, "center");
}
}
google.maps.event.addDomListener(window, 'load', initialize);
// This example creates circles on the map, representing
// populations in North America.
// First, create an object containing LatLng and population for each city.
var citymap = {};
citymap['chicago'] = {
center: new google.maps.LatLng(41.878113, -87.629798),
population: 2714856
};
citymap['newyork'] = {
center: new google.maps.LatLng(40.714352, -74.005973),
population: 8405837
};
citymap['losangeles'] = {
center: new google.maps.LatLng(34.052234, -118.243684),
population: 3857799
};
citymap['vancouver'] = {
center: new google.maps.LatLng(49.25, -123.1),
population: 603502
};
var cityCircle;
html,
body,
#map-canvas {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk" ></script>
<div id="map-canvas"></div>