-1

is that I have an error with google maps, I need to update the user's position every 5 seconds centering the map, but when using setCenter it leaves indefinite error because the map does not exist, they could guide me. Thank you

function cargarMapa() {
      var MY_MAPTYPE_ID = 'custom_style';
       var featureOpts =[
      {"featureType": "administrative","elementType": "geometry","stylers": [{"visibility": "off"}]},
      {"featureType": "poi","stylers": [{"visibility": "off"}]},
      {"featureType": "road","elementType": "labels.icon","stylers": [{"visibility": "off"}]},
      {"featureType": "transit","stylers": [{"visibility": "off"}]}];

        var map = new google.maps.Map(document.getElementById('map'), {
          center: {lat: 6.268688560352701, lng: -75.59639199999998},
          zoom: 17,
          disableDefaultUI: true,
          zoomControl: true,
          zoomControlOptions: {
              position: google.maps.ControlPosition.RIGHT_CENTER
          }
        });
        var styledMapOptions = {name: 'mimap'};
        if (navigator.geolocation) {
          navigator.geolocation.getCurrentPosition(function(position) {
              var pos = {
                lat: position.coords.latitude,
                lng: position.coords.longitude
              };

            var marker = new google.maps.Marker({
                position: pos,
                map: map,
                icon:"./ico/bicimini.png"
              });
              map.setCenter(pos);
          });
        } else {
          // Browser doesn't support Geolocation
          handleLocationError(false, infoWindow, map.getCenter());
        }

        var customMapType = new google.maps.StyledMapType(featureOpts, styledMapOptions);
         map.mapTypes.set(MY_MAPTYPE_ID, customMapType);

}
function actualizar() {

  navigator.geolocation.getCurrentPosition(function(position) {
      var pos = {
        lat: position.coords.latitude,
        lng: position.coords.longitude,
        altitude: position.coords.altitude,
        altitudeAccuracy: position.coords.altitudeAccuracy,
        heading: position.coords.heading,
        speed: position.coords.speed,
        timestamp: position.coords.timestamp
      };
      //alert(JSON.stringify(pos, null, 4));
      map.setCenter(pos);//indefinidad
  });
}
geocodezip
  • 158,664
  • 13
  • 220
  • 245
Andres Henao
  • 11
  • 10
  • Maybe use `watchPosition`? Related questions: [watchPosition() and getCurrentPosition](https://stackoverflow.com/questions/13324165/watchposition-and-getcurrentposition), [watchPosition() vs getCurrentPosition() with setTimeout](https://stackoverflow.com/questions/1948952/watchposition-vs-getcurrentposition-with-settimeout) – geocodezip Apr 20 '19 at 18:29

1 Answers1

0

The variable map is within the scope of cargarMapa, and won't be accessible within actualizar. Declare map outside of cargarMapa like so...

let map

function cargarMapa() {
  ...
  map = new google.maps.Map(...)
}

function actualizar() {
  ...
  map.setCenter(pos);
}