如何从地理编码地址找到经纬度? [英] How to find the lat lng from a geocoded address?

查看:117
本文介绍了如何从地理编码地址找到经纬度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个Gmap,以便人们对地址进行地理编码并找到相应的经纬度。我想用我已经创建的表单来看这个。另外,我希望能够在拖动标记时更改坐标。

I'm trying to create a Gmap to allow people to geocode an address and find the respective lat lng. I would like to see this in the forms I've already created. Also, I would like to have the capability of changing the coordinates as the marker is dragged.

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


推荐答案

好的,这是代码,想要。这可能不是那么优雅,但它的工作原理。我使用getPosition()方法获取坐标,只需点击地理编码标记即可!

Ok... here's the code that makes what I wanted to. It might not be that elegant, but it works. I used the getPosition() method to get the coordinates, just have to click in the geocoded marker!

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

这篇关于如何从地理编码地址找到经纬度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆