how to keep the position of the map to the KML layer rather than current position of the user?

StackOverflow https://stackoverflow.com/questions/22584678

  •  19-06-2023
  •  | 
  •  

Question

The below java script basically imports a KML layer file on the map and also displays the current location of the user using geolocation in html 5.

The problem is that it zooms to the current location of the user. I do not want that to happen i still want the focus to be on the KML layer routes rather than the current location of the user.

var map;

function initialize() {
  var abby = new google.maps.LatLng(49.051078,-122.314221);
  var mapOptions = {
    zoom: 11,
    center: abby
  }

  map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
  var ctaLayer = new google.maps.KmlLayer({
    url:urllink,
    preserveViewport: true


  });
  ctaLayer.setMap(map);

  if(navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
      var pos = new google.maps.LatLng(position.coords.latitude,
                                       position.coords.longitude);

      var infowindow = new google.maps.InfoWindow({
        map: map,
        position: pos,
        content: 'Location found using HTML5.'
      });

   // map.setCenter(pos);
    }, function() {
      handleNoGeolocation(true);
    });
  } else {
    // Browser doesn't support Geolocation
    handleNoGeolocation(false);
  }
}

function handleNoGeolocation(errorFlag) {
  if (errorFlag) {
    var content = 'Error: The Geolocation service failed.';
  } else {
    var content = 'Error: Your browser doesn\'t support geolocation.';
  }

  var options = {
    map: map,
    position: new google.maps.LatLng(60, 105),
    content: content
  };

  var infowindow = new google.maps.InfoWindow(options);
  map.setCenter(options.position);
}


google.maps.event.addDomListener(window, 'load', initialize);
Was it helpful?

Solution

Comment out the line:

map.setCenter(pos);

which set center to the position of user.

Update: And you will have to add infowindow option:

disableAutoPan: true

to disable auto-pan on open. By default, the info window will pan the map so that it is fully visible when it opens.

OTHER TIPS

Remove the {preserveViewport: true} to the KmlLayer.

var ctaLayer = new google.maps.KmlLayer({
  url:urllink
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top