移动谷歌地图API中的圆形。

4

好的,我正在学习Google Maps API,并尝试根据经度和纬度实时移动一个点(就像Google Maps使用蓝点一样)。

除非我弄错了,否则看起来我必须绘制自己的圆并更新其位置。

问题在于,我无法使圆形得到更新。

这是我的代码:

var citymap = {};
citymap['chicago'] = {
  center: new google.maps.LatLng(41.878113, -87.629798),
  population: 2842518
};

var cityCircle;

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 population by a factor of 20.
  for (var city in citymap) {
    var populationOptions = {
      strokeColor: '#fff',
      strokeOpacity: 0.8,
      strokeWeight: 2,
      fillColor: '#fff',
      map: map,
      center: citymap[city].center,
      radius: 50000,

    };
    // Add the circle for this city to the map.
    cityCircle = new google.maps.Circle(populationOptions);
    var a=0;
    window.setInterval(function(){
        var p = populationOptions.center;
        var g = p.lat()+500;
        var m = p.lng()+500;    
        cityCircle.setCenter(new google.maps.LatLng(g,m));
        cityCircle.setRadius(a+=10);
    }, 50);
  }
}

我其实很惊讶谷歌的api没有直接暴露那个蓝色标记,这样你就可以在需要时移动它。

1个回答

8

你的更新函数不正确。任何有效的纬度加上500都将超出地图范围。更合理的数字应该是0.1度。另外,你应该使用cityCircle.getCenter()代替populationOptions.center。

// Add the circle for this city to the map.
cityCircle = new google.maps.Circle(populationOptions);
var a=50000;
window.setInterval(function(){
    var p = cityCircle.getCenter();
    var g = p.lat()+0.1;
    var m = p.lng()+0.1;    
    cityCircle.setCenter(new google.maps.LatLng(g,m));
    cityCircle.setRadius(a+=10);
}, 500);

working example

code snippet:

html,
body,
#map-canvas {
  height: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<div id="map-canvas"></div>
<script>
  var citymap = {};
  citymap['chicago'] = {
    center: new google.maps.LatLng(41.878113, -87.629798),
    population: 2842518
  };

  var cityCircle;

  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 population by a factor of 20.
    for (var city in citymap) {
      var populationOptions = {
        strokeColor: '#FF0000',
        strokeOpacity: 0.8,
        strokeWeight: 2,
        fillColor: '#FF0000',
        fillOpacity: 0.35,
        map: map,
        center: citymap[city].center,
        radius: 50000

      };
      // Add the circle for this city to the map.
      cityCircle = new google.maps.Circle(populationOptions);
      // move the circle
      var a = 50000;
      window.setInterval(function() {
        var p = cityCircle.getCenter();
        var g = p.lat() + 0.1;
        var m = p.lng() + 0.1;
        cityCircle.setCenter(new google.maps.LatLng(g, m));
        cityCircle.setRadius(a += 10);
      }, 500);
    }
  }

  google.maps.event.addDomListener(window, 'load', initialize);
</script>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接