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

これを試してみてください。同じ問題がありましたが、ビングマップで。最初に、最初の場所の緯度と経度をハードコーディングする必要があります。

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 に答える