【问题标题】:How to find the lat lng from a geocoded address?如何从地理编码地址中找到 lat lng?
【发布时间】:2012-09-01 10:32:43
【问题描述】:

我正在尝试创建一个 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>

【问题讨论】:

  • lat/lng不是已经在results[0].geometry.location里面了吗? :)
  • 请您解释一下好吗?我对这个 Gmaps Gecoding '东西'很陌生......如果值已经存在,我该如何在表格中显示它?非常感谢!

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


【解决方案1】:

在地理编码器回调中执行以下操作:

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

Working example

【讨论】:

  • 非常感谢,拉里!现在,我如何拖动标记并获取它的新坐标?另外,您能否解释一下什么是回调(在这种情况下)?你知道我在哪里可以找到更多关于谷歌地理编码器的例子和解释(不是来自谷歌文档?
  • 我可以建议先进行一些研究吗?关于从可拖动标记获取坐标的问题已被多次询问(此处和 Google Maps API v3 组)Drag marker, get new coordinatesCallback function
  • 好的,我已经看到了一些关于此的问题/示例,但我会搜索更多!
【解决方案2】:

好的...这是实现我想要的代码。它可能不是那么优雅,但它确实有效。我使用 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-14
    相关资源
    最近更新 更多