2012-08-14 82 views
1

我試圖創建一個Gmap來允許人們對地址進行地址解析並找到相應的經緯度。我想用我已經創建的表單來看這個。另外,我希望能夠在拖動標記時更改座標。如何從地理編碼地址找到經緯度?

<!DOCTYPE html> 
<html> 
<head> 
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no"> 
    <meta charset="UTF-8"> 
    <title>Geocoder 10</title> 
<style> 
#map_canvas { width:400px; height:450px; } 
</style> 
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"> 
</script> 
<script type="text/javascript"> 

var geocoder; 
var map; 

function initialize() { 
    geocoder = new google.maps.Geocoder(); 
    var latlng = new google.maps.LatLng(-30.070, -51.190); 
    var myOptions = { 
     zoom: 9, 
     center: latlng, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    } 
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 
} 

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); 
     } 
    }); 
} 

</script> 
</head> 

<body onload="initialize()"> 
<div id="map_canvas"></div> 
<div> 
    <input id="address" type="textbox" value=""> 
    <input type="button" value="Localizar!" onclick="codeAddress()"><br> 
    Latitude: <input type="text" id="lat"><br> 
    Longitude: <input type="text" id="lng"><br> 
    </div> 
</body> 
</html> 
+1

'結果[0] .geometry.location'裏面是不是'lat/lng'? :) – 2012-08-14 01:50:11

+0

你能解釋一下嗎?我對這個Gmaps Gecoding的'東西'很新穎......如果價值已經存在,我該如何在表單中展示它?非常感謝! – 2012-08-14 01:58:00

+1

讓我舉一個例子。 http://gmaps-samples.googlecode.com/svn/trunk/geocoder/singlegeocode.html/ – Jessedc 2012-08-14 02:02:31

回答

1

好吧......這裏的代碼,使我想做的。這可能不是那麼優雅,但它的工作原理。我使用getPosition()方法獲取座標,只需點擊地理編碼標記!

<!DOCTYPE html> 
<html> 
<head> 
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no"> 
    <meta charset="UTF-8"> 
    <title>Geocoder</title> 
<style> 
#map_canvas { width:400px; height:450px; } 
</style> 
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"> 
</script> 
<script type="text/javascript"> 

var geocoder; 
var map; 

function initialize() { 
    geocoder = new google.maps.Geocoder(); 
    var latlng = new google.maps.LatLng(-30.070, -51.190); 
    var myOptions = { 
     zoom: 9, 
     center: latlng, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    } 
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 
} 

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('coords').value = marker.getPosition(); 
     }); 
    }); 
} 

</script> 
</head> 

<body onload="initialize()"> 
<div id="map_canvas"></div> 
<div id="info"></div> 
<div> 
    <input id="address" type="textbox" value=""> 
    <input type="button" value="Localizar!" onclick="codeAddress()"><br> 
    <input type="textbox" id="coords"> 
    </div> 
</body> 
</html> 
2

內部地址解析器回調做到這一點:

document.getElementById('lat').value = results[0].geometry.location.lat(); 
document.getElementById('lng').value = results[0].geometry.location.lng(); 

Working example

+0

非常感謝Larry!現在,我怎麼能拖動標記並獲得新的座標?另外,你能解釋一下什麼是回調(在這種情況下)?你知道我在哪裏可以找到更多關於谷歌地理編碼器的例子和解釋(不是來自谷歌文檔?) – 2012-08-14 22:57:30

+0

我可能首先提出一些研究嗎?關於從可拖動標記獲取座標的問題已被多次詢問(這裏和谷歌地圖API v3組)[拖動標記,獲取新座標](http://stackoverflow.com/questions/4645151/save-new-position-of-marker-from-draggable-marker-google-maps-v3),[回調函數](http://stackoverflow.com/questions/824234/what-is-a-callback-function) – geocodezip 2012-08-14 23:42:12

+0

好吧,我已經看到了一些關於這個問題/例子,但我會尋找更多! – 2012-08-15 01:25:43

相關問題