2012-08-25 41 views
0

我通过地址获取纬度和经度。所有工作都很好,我得到的地图显示完全没有问题,但我想显示高度和经度。使用getElementById时不显示lat和lng

这里是我的代码,我在身体

function codeAddress() { 
    var address = document.getElementById("address").value; 
    geocoder.geocode({ 'address': address}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
      map.setCenter(results[0].geometry.location); 
      var marker = new google.maps.Marker({ 
       map: map, 
       position: results[0].geometry.location, 
       draggable: true, 
      }); 

     } else { 
      alert("Geocode was not successful for the following reason: " + status); 
     } 
     google.maps.event.addListener(marker, 'click', function() { 
      document.getElementById('lat').value = results[0].geometry.location.lat(); 
      document.getElementById('lng').value = results[0].geometry.location.lng(); 
     }); 
    }); 
} 

这里初始化的用法:

<div id="mapholder"></div> 
      </p> 
      <div id="map_canvas"></div> 
      <div id="info"></div> 
      <div> 
       <input id="address" type="textbox" value="הזן כתובת"> 
       <input type="button" value="מצא את מיקומך" onclick="codeAddress()"><br> 
       <input type="button" value="מצא קופונים באזורך" onclick="showPositionCoupons(32.105892, 35.198483)"><br> 
       Latitude: <input type="text" id="lat"><br> 
       Longitude: <input type="text" id="lng"><br> 
      </div> 
+1

没有你检查结果[0] .geometry.location.lat()'的内容?点击事件是否正在启动?如果您正在使用萤火虫检查'console.log(results [0])' – Peter

回答

0

好了,发现代码伟大的工程

function updateCoordinates(latlng) 
{ 
    if(latlng) 
    { 
    document.getElementById('lat').value = latlng.lat(); 
    document.getElementById('lng').value = latlng.lng(); 
    } 
} 

function codeAddress() { 
    var address = document.getElementById("address").value; 
    geocoder.geocode({ 'address': address}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
      map.setCenter(results[0].geometry.location); 
      updateCoordinates(results[0].geometry.location); 
      if (marker) marker.setMap(null); 
      marker = new google.maps.Marker({ 
       map: map, 
       position: results[0].geometry.location, 
       draggable: true 
      }); 

      google.maps.event.addListener(marker, "dragend", function() { 
       updateCoordinates(marker.getPosition()); 
      }); 

     } else { 
      alert("Geocode was not successful for the following reason: " + status); 
     } 
    }); 
} 
+0

请将您的答案标记为“已接受”,这样我们就不会浪费时间解决已经解决的问题。 – Marcelo

+0

说我需要等2天。 –

相关问题