map.setCenter() function is not working properly

后端 未结 2 1847
名媛妹妹
名媛妹妹 2021-01-18 01:31

Here\'s code...




        
相关标签:
2条回答
  • 2021-01-18 02:05

    You'll need to set center like this...

    map.setCenter(new google.maps.LatLng(latitude, longitude));
    

    Also you are setting center before initializing map and so its throwing an error.

    Demo Fiddle

    Here's updated JS.

         var geocoder;
    
     var map;
    
     function initialize() {
    
         geocoder = new google.maps.Geocoder();
    
         var address = "new delhi";
    
         geocoder.geocode({
             'address': address
         }, function (results, status) {
    
             if (status == google.maps.GeocoderStatus.OK) {
    
                 var latitude = results[0].geometry.location.lat();
    
                 var longitude = results[0].geometry.location.lng();
    
                 alert(latitude);
    
                 alert(longitude);
    
    
    
    
                 var latlng = new google.maps.LatLng(latitude, longitude);
    
                 var mapOptions = {
    
                     zoom: 8,
    
                     center: latlng,
    
                     mapTypeId: google.maps.MapTypeId.ROADMAP
    
                 }
    
                 map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
    
                 var latlng = new google.maps.LatLng(latitude, longitude);
                 map.setCenter(latlng);
    
                 var marker = new google.maps.Marker({
    
                     map: map,
    
                     position: latlng,
                     title: 'Hello World!'
    
                 });
             }
    
         });
     }
     google.maps.event.addDomListener(window, 'load', initialize);
    
    0 讨论(0)
  • 2021-01-18 02:09

    I am using gmaps.js library.

    I was getting maximum stack size exceeded error when i tried to recenter the map using map.setCenter(new google.maps.LatLng(latitude, longitude));

    so i tried just this map.setCenter(latitude, longitude); and it worked for me.

    0 讨论(0)
提交回复
热议问题