结合谷歌地图地理编码和单张地图接收错误:无效的经纬度对象(Combining Google Maps

2019-09-23 16:32发布

我使用谷歌地图地理编码抢地址的经度和纬度。 然后我采取这一地址,并使用单张和平移地图的纬度+经度坐标。 不过,我从萤火虫说收到错误Error: Invalid LatLng object: (-33.8674869, 151.20699020000006, undefined) 。 我知道我的变量geolocation返回-33.8674869, 151.20699020000006而不是不确定的。 是什么造成这个问题?

 <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> 

Answer 1:

替换此:

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

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

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

    map.panTo([geolocation]);

接着就,随即:

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

说明:
您分配一个字符串数组第一价值,这是一样的:

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

该字符串将不进行评估,它将保持1串,但你需要有2个浮标阵列:

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

undefined是提供给阵列的缺少第二项panTo()



文章来源: Combining Google Maps Geocoder and Leaflet Map Receiving Error: Invalid LatLng object