在Google地图上绘制“自定义”圆圈

时间:2013-12-06 09:53:31

标签: google-maps google-maps-api-3

我正在尝试绘制带有圆圈的谷歌地图......没问题。这里很好的例子......

https://developers.google.com/maps/documentation/javascript/examples/circle-simple

但是,我想要做的是用自定义图像替换红色圆圈,使用同心圆,用户可以在地图上移动。像这样:

Google Map with Concentric Circles

我需要在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>

1 个答案:

答案 0 :(得分:3)

在这里归功于邓肯。正确地指出我应该使用

firstCircle.setCenter(fourthCircle.getCenter());

完美地运作!