【问题标题】:map.setCenter() function is not working properlymap.setCenter() 函数无法正常工作
【发布时间】:2013-10-23 08:57:01
【问题描述】:

这是代码...

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

【问题讨论】:

  • GLatLng 是 Google Maps API v2,已弃用(由于很快停止工作)。使用google.maps.LatLng 代替@Nikhil 的回答

标签: javascript google-maps google-maps-api-3


【解决方案1】:

你需要像这样设置中心...

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

而且你在初始化地图之前设置了中心,所以它会抛出一个错误。

Demo Fiddle

这里是更新的 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】:

我正在使用 gmaps.js 库。

当我尝试使用 map.setCenter(new google.maps.LatLng(latitude, longitude)) 重新定位地图时,出现最大堆栈大小超出错误;

所以我只尝试了这个 map.setCenter(latitude, longitude); 它对我有用。

【讨论】:

    猜你喜欢
    • 2017-12-15
    • 2013-03-27
    • 2012-08-15
    • 2013-11-05
    • 1970-01-01
    • 2022-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多