如何从地理编码的地址中找到lat lng?

时间:2012-08-14 01:43:28

标签: javascript google-maps google-maps-api-3

我正在尝试创建一个Gmap,以允许人们对地址进行地理编码并找到相应的lat lng。我希望以我已经创建的形式看到这一点。此外,我希望能够在拖动标记时更改坐标。

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

2 个答案:

答案 0 :(得分:2)

在地理编码器回拨内部执行此操作:

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

Working example

答案 1 :(得分: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>