Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

moving a circle in google maps api

Ok so I'm learning the google maps API and I'm trying to get a point to move real time based on the long and lat(exactly how google maps works with the blue dot)

No unless I'm mistaken it looks like I have to draw my own circle and update it's position.

The problem is I just can't get the circle to get updated.

here is my code:

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);
  }
}

I was actually quite surprised google's api doesn't just expose that blue marker and you can just move it when you need to.

like image 633
dbarnes Avatar asked Feb 15 '23 15:02

dbarnes


1 Answers

Your update function is incorrect. Any valid latitude + 500 will be off the map. A more reasonable number would be 0.1 degrees. Also, instead of populationOptions.center you should use cityCircle.getCenter().

// 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>
like image 167
geocodezip Avatar answered Feb 27 '23 12:02

geocodezip