结合Google地图地理编码器和传单地图接收错误:LatLng对象无效 [英] Combining Google Maps Geocoder and Leaflet Map Receiving Error: Invalid LatLng object

查看:303
本文介绍了结合Google地图地理编码器和传单地图接收错误:LatLng对象无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Google地图地理编码器获取地址的经纬度。然后,我采用该地址并使用Leaflet并将地图平移至纬度+经度坐标。但是,我收到Firebug发出的一个错误消息:错误:无效的LatLng对象:(-33.8674869,151.20699020000006,undefined)。我知道我的变量 geolocation 返回 -33.8674869,151.20699020000006 但不是未定义的。是什么导致了这个问题?

 < body onload =initialize()> 
< div id =result>< / div>
< div>
< input id =addresstype =textboxvalue =Sydney,NSW>
< input type =buttonvalue =Geocodeonclick =codeAddress()>
< / div>

< div id =map>< / div>

< script type =text / javascript>

var map = L.map('map')。setView([33.7489954,-84.3879824],13);

var geocoder;

函数initialize(){
geocoder = new google.maps.Geocoder();
}

函数codeAddress(){
var address = document.getElementById('address')。value;
geocoder.geocode({'address':address},function(results,status){
if(status == google.maps.GeocoderStatus.OK){

var geolocation = String(results [0] .geometry.location);

var geolocation = geolocation.replace('(','');

var geolocation = geolocation。替换(')','');

map.panTo([geolocation]);

} else {
$('#result')。 html('Geocode不成功,原因如下:'+ status);
}
});
};

.tileLayer('http:// {s} .tile.cloudmade.com / apikeyputhere / 997/256 / {z} / {x} / {y} .png',{
maxZoom:18
})。addTo(map);

< / script>


解决方案

替换为:

  var geolocation = String(results [0] .geometry.location); 

var geolocation = geolocation.replace('(','');

var geolocation = geolocation.replace(')','');

map.panTo([geolocation]);



  map.panTo([结果[0] .geometry.location.lat(),结果[0] .geometry.location.lng()]); 

说明:

您将字符串分配给第一个数组值,它与以下内容相同:

  geolocation = new Array(' -  33.8674869,151.20699020000006')//包含1字符串

字符串不会被评估,它仍然是1个字符串,但是您需要一个数组2浮点数:

  geolocation = new Array(-33.8674869,151.20699020000006)//包含2个浮点数
$ b $ 未定义是提供给<$ c $的数组中缺少的第二项c> panTo()


I'm using Google Maps Geocoder to grab the latitude and longitude of an address. I'm then taking that address and using Leaflet and panning the map to the latitude + longitude coordinates. However, I'm receiving an error from Firebug saying Error: Invalid LatLng object: (-33.8674869, 151.20699020000006, undefined). I know my variable geolocation returns -33.8674869, 151.20699020000006 but not the undefined. What's causing the problem?

 <body onload="initialize()">
  <div id="result"></div>
 <div>
  <input id="address" type="textbox" value="Sydney, NSW">
  <input type="button" value="Geocode" onclick="codeAddress()">
 </div>

 <div id="map"></div>

 <script type="text/javascript">

 var map = L.map('map').setView([33.7489954, -84.3879824], 13);

  var geocoder;

  function initialize() {
    geocoder = new google.maps.Geocoder();     
  }

  function codeAddress() {
    var address = document.getElementById('address').value;
    geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {

        var geolocation = String(results[0].geometry.location);

        var geolocation = geolocation.replace('(','');

        var geolocation = geolocation.replace(')','');      

        map.panTo([geolocation]);

      } else {
        $('#result').html('Geocode was not successful for the following reason: ' + status);
      }
    });
  };

L.tileLayer('http://{s}.tile.cloudmade.com/apikeyputhere/997/256/{z}/{x}/{y}.png', {
maxZoom: 18
}).addTo(map);

</script> 

解决方案

Replace this:

    var geolocation = String(results[0].geometry.location);

    var geolocation = geolocation.replace('(','');

    var geolocation = geolocation.replace(')','');      

    map.panTo([geolocation]);

with that:

map.panTo([results[0].geometry.location.lat(),results[0].geometry.location.lng()]);

Explanation:
You assign a string to the first array-value, it's the same as:

geolocation=new Array('-33.8674869, 151.20699020000006')//contains 1 string

The string will not be evaluated, it will remain 1 string, but you need an array with 2 floats:

geolocation=new Array(-33.8674869, 151.20699020000006)//contains 2 floats

The undefined is the missing 2nd item of the array provided to panTo()

这篇关于结合Google地图地理编码器和传单地图接收错误:LatLng对象无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆