map.setCenter() function is not working properly

泄露秘密 提交于 2019-12-30 18:28:08

问题


Here's code...

<script src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script>
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);
            map.setCenter(new GLatLng(latitude, longitude));

            var marker = new google.maps.Marker({
                map: map,
                position: results[0].geometry.location
            });
            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);
        } 
    });
    google.maps.event.addDomListener(window, 'load', initialize);    
}
</script>

回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/19537225/map-setcenter-function-is-not-working-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!