2012-08-14 90 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

相关问题