2

Google Maps Geocoder を使用して、住所の緯度と経度を取得しています。次に、その住所を取得して Leaflet を使用し、マップを緯度 + 経度の座標にパンします。ただし、Firebug から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> 
4

1 に答える 1

5

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

于 2012-10-07T23:30:35.503 に答える