0

需要在加载时将地图居中设置为位置数组中字符串中设置的值。当我尝试运行它时,它将浏览器中的 html 文件重命名为“London,New%20York”。Google Maps API 以其他方式工作。

我在这里错过了一些基础吗?

<script type="text/javascript">
            var geocoder = new google.maps.Geocoder();
            var map;
            var location = ["London", "New York"];
            var pos;

            function initialize()
            {
                google.maps.visualRefresh = true;
                getGeoCode();
                var mapOptions = {
                zoom: 8,
                center: pos,
                mapTypeId: google.maps.MapTypeId.ROADMAP
                }
                map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
             }

            function getGeoCode()
            {
                geocoder.geocode({'address': location[1]}, function(results, status){
                if(status == google.maps.GeocoderStatus.OK)
                {
                    pos = results[0].geometry.location;
                }
                else
                {
                    alert("Not found");
                }
              });
            }

            google.maps.event.addDomListener(window, 'load', initialize);
  </script>
4

3 回答 3

3

问题是“位置”是javascript中的保留字

当您将其设置为

var location = ["London", "New York"];

您更改页面的 url。

地理编码器的异步行为也有问题,需要在回调函数中使用结果。这对我有用:

<script type="text/javascript">
            var geocoder = new google.maps.Geocoder();
            var map;
            var locations = ["London", "New York"];
            var pos;

            function initialize()
            {
                google.maps.visualRefresh = true;
                getGeoCode();
             }

            function getGeoCode()
            {
                geocoder.geocode({'address': locations[1]}, function(results, status){
                if(status == google.maps.GeocoderStatus.OK)
                {
                    pos = results[0].geometry.location;
                    var mapOptions = {
                      zoom: 8,
                      center: pos,
                      mapTypeId: google.maps.MapTypeId.ROADMAP
                    }
                    map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
                }
                else
                {
                    alert("Not found");
                }
              });
            }

            google.maps.event.addDomListener(window, 'load', initialize);
  </script>
于 2013-10-18T17:25:11.033 回答
0

试试这个,我有同样的问题。但是在 bing 地图中。首先,您必须硬编码初始位置的纬度和经度。

var startingLat = 43.0729484;  // view of Madison
var startingLng = -89.3866882;  // view of Madison
var startingZoom = 15;

var mapOptions = {
                zoom: 8,
                center: new Microsoft.Maps.Location(startingLat, startingLng)
                mapTypeId: google.maps.MapTypeId.ROADMAP
                }

希望你有一个想法。

编辑:

//first declare   
  var viewBoundaries;
      //(this will work for bing maps)
    viewBoundaries = Microsoft.Maps.LocationRect.fromLocations(your locationAry);
      map.setView({ bounds: viewBoundaries });
于 2013-10-18T17:11:22.920 回答
0

替换你的两个功能

function initialize()
     {
          google.maps.visualRefresh = true;

          geocoder = new google.maps.Geocoder();
                    var mapOptions = {
                        zoom: 10,

           };
          map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
          getGeoCode();
     }

     function getGeoCode()
        {
                        geocoder.geocode({'address': location[1]}, function(results, status){
                        if(status == google.maps.GeocoderStatus.OK)
                        {
                            pos = results[0].geometry.location;
                            map.setCenter(results[0].geometry.location);
                        }
                        else
                        {
                            alert("Not found");
                        }
                      });
        }

希望这有帮助

于 2013-10-18T17:23:50.477 回答