简体   繁体   English

合并Google Maps Geocoder和Leaflet Map接收错误:无效的LatLng对象

[英]Combining Google Maps Geocoder and Leaflet Map Receiving Error: Invalid LatLng object

I'm using Google Maps Geocoder to grab the latitude and longitude of an address. 我正在使用Google Maps Geocoder来获取地址的经度和纬度。 I'm then taking that address and using Leaflet and panning the map to the latitude + longitude coordinates. 然后,我要获取该地址并使用Leaflet,然后将地图平移至纬度+经度坐标。 However, I'm receiving an error from Firebug saying Error: Invalid LatLng object: (-33.8674869, 151.20699020000006, undefined) . 但是,我从Firebug收到一条错误Error: Invalid LatLng object: (-33.8674869, 151.20699020000006, undefined) I know my variable geolocation returns -33.8674869, 151.20699020000006 but not the undefined. 我知道我的变量geolocation返回-33.8674869, 151.20699020000006但未定义。 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: 该字符串将不被求值,它将保留为1个字符串,但是您需要一个具有2个浮点数的数组:

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

The undefined is the missing 2nd item of the array provided to panTo() undefined是提供给panTo()的数组的缺少的第二项

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 带有Leaflet映射的无效LatLng对象 - Invalid LatLng object with Leaflet map Leaflet错误:无效的LatLng对象:(,undefined) - Leaflet error: Invalid LatLng object: ( , undefined) leaflet.js显示空地图:未捕获错误:无效的LatLng对象:(NaN,NaN) - leaflet.js is showing empty map: Uncaught Error: Invalid LatLng object: (NaN, NaN) 尝试使用传单映射坐标时获取无效的LatLng对象 - Getting Invalid LatLng object when trying to map coordinates using leaflet leaflet.js:未捕获的错误:无效的LatLng对象:(NaN,NaN) - leaflet.js: Uncaught Error: Invalid LatLng object: (NaN, NaN) Leaflet Uncaught Error:无效的LatLng对象:(NaN,NaN) - Leaflet Uncaught Error: Invalid LatLng object: (NaN, NaN) Invalid LatLng object: (37.42216, undefined) 在我的 react-redux 应用程序上渲染谷歌地图时出错 - Invalid LatLng object: (37.42216, undefined) error when rendering google map on my react-redux app 如何在Google Maps API中使用Geocoder获取城市的Latlng价值? - How to get latlng value of a city using geocoder in google maps api? Google 地图错误:不是 LatLng 或 LatLngLiteral:不是 Object - 似乎与“边界”相关 - Google Maps Error: not a LatLng or LatLngLiteral: not an Object - seems to be 'bounds' related 如何在传单地图中修复无效的LatLng对象:(NaN,NaN)? - How do I fix Invalid LatLng object: (NaN, NaN) in Leaflet map?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM