【发布时间】:2013-12-23 15:00:55
【问题描述】:
我正在尝试绘制带有圆圈的谷歌地图......没问题。很好的简单例子......
https://developers.google.com/maps/documentation/javascript/examples/circle-simple
但是,我想做的是用自定义图像替换红色圆圈,用户可以在地图上移动的同心圆。像这样:
我需要 220nm、470nm、720nm 和 970nm 的四个同心环
我查看了圆圈的属性,似乎没有办法让圆圈成为图像...
我可以看到如何生成 4 个只有边框且没有内部颜色的圆圈,这可能是解决此问题的一种方法,但我不知道如何让它们全部一致移动。
有谁知道是否可以将圆圈制作成图像,或者我是否试图以错误的方式这样做?
我可能需要使用 KML 图层吗?
任何帮助将不胜感激!
更新:
我现在决定尝试“4 圈”方法。即在地图上添加 4 个单独的圆圈,使最后一个可拖动,并在最后一个尝试更改所有其他圆圈的“中心”的“center_changed”侦听器...
但是移动第 4 个圆圈不会移动第一个圆圈...
有什么想法吗?
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Circles</title>
<style>
html, body, #map-canvas {
height: 100%;
margin: 0px;
padding: 0px
}
</style>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script>
// This example creates circles on the map, representing
// populations in the United States.
// First, create an object containing LatLng and population for each city.
var citymap = {};
citymap['firstcircle'] = {
center: new google.maps.LatLng(51.511214, -0.119824),
radius: 407440
};
citymap['secondcircle'] = {
center: new google.maps.LatLng(51.511214, -0.119824),
radius: 2000000
};
citymap['thirdcircle'] = {
center: new google.maps.LatLng(51.511214, -0.119824),
radius: 3000000
};
citymap['fourthcircle'] = {
center: new google.maps.LatLng(51.511214, -0.119824),
radius: 1796440
};
var cityCircle;
function initialize() {
// Create the map.
var mapOptions = {
zoom: 4,
center: new google.maps.LatLng(51.511214, -0.119824),
mapTypeId: google.maps.MapTypeId.TERRAIN
};
var map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
// Create circle 1
var firstCircleOptions = {
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 2,
map: map,
center: citymap['firstcircle'].center,
radius: citymap['firstcircle'].radius,
draggable: true
};
// Add the circle for this city to the map.
firstCircle = new google.maps.Circle(firstCircleOptions)
// Create circle 4
var fourthCircleOptions = {
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 2,
map: map,
center: citymap['fourthcircle'].center,
radius: citymap['fourthcircle'].radius,
draggable: true
};
// Add the circle for this city to the map.
fourthCircle = new google.maps.Circle(fourthCircleOptions)
google.maps.event.addListener(fourthCircle, 'center_changed', function () {
firstCircle.center == fourthCircle.center;
//alert("circle moved");
});
google.maps.event.addListener(fourthCircle, 'dragend', function () {
alert(fourthCircle.center);
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="map-canvas"></div>
</body>
【问题讨论】:
-
尝试改用
firstCircle.setCenter(fourthCircle.getCenter()); -
@Duncan 不错。谢了哥们!现场。
标签: google-maps google-maps-api-3