0

JAVASCRIPT

我确定这是一个非常简单的问题。

我正在修改下面的行并想用一个变量替换 (53, 0) 。但是,创建的名为“result”的变量等于“(34, 2)”,即它已经有括号括起来。

center: new google.maps.LatLng.(53, 0)

我试过了

center: new google.maps.LatLng.(result)
center: new google.maps.LatLng.result
center: new google.maps.LatLng.({result})

它没有工作!

完整代码

var geocoder = new google.maps.Geocoder();

  if (geocoder) {
     geocoder.geocode({ 'address': address }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {

          window.result = results[0].geometry.location;

          alert(result);



        } 
        else {
          alert('nothing');
        }
     });
  }

  function load() {

    map = new google.maps.Map(document.getElementById("map"), {
    center: new google.maps.LatLng.(53, 0),
    zoom: 10,
    mapTypeId: 'roadmap',
    mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU}
  });

}

作为参考, alert(result) 返回

(53.8622017,-2.405405999999971)

4

3 回答 3

2

你把。在 LatLng 之后,创建 latlng 对象的正确方法是这样尝试

center:new google.maps.LatLng(-34.397, 150.644);

这里 。定义包级别,最后一个 latLng 是类

就像谷歌在该地图中是顶级的,并且在该地图中定义了 LatLng。

所以 LatLng 有 2 个参数(纬度和经度)

于 2011-06-10T13:49:30.173 回答
0

试试新的 google.maps.LatLng(result[0], result[1])?请张贴创建“结果”变量的位置。

于 2011-06-10T13:48:35.793 回答
0

为什么不在响应中设置地图中心

if (status == google.maps.GeocoderStatus.OK) {

         map.setCenter(results[0].geometry.location);

  }
于 2011-06-10T14:08:10.800 回答