得到整齐格式的地址:Latlng - >地址谷歌地图

时间:2011-07-19 21:43:29

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

我正在制作一个利用Google Maps地理编码器对象查找用户输入地址的页面。我能够找到lat / lng点,但我想格式化地址(清理它),这样我就可以将Lat,Lng和Address存储在MySQL数据库的3个独立字段中。这是我目前的代码:

也可以随意批评安全范围内的当前代码,我是Gmaps API的新手,并且不熟悉js

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Basic Map</title>


<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=true_or_false&amp;key=ABQIAAAAKgJa4e_5XNFRJJF7MsI1eRQN1RsnicNCcG4bidjiZE76b9vSTBTCbiFNb-LGPKHeiBuV_2yfnWNSTA" type="text/javascript">
</script>



</head>

<body>


Input address Here: <input type="text" name="locate_address" id="locate_address" />
<input type="button" name="locate_address" value="Locate Address" onclick="locateAddress()" />
<div>
<p id="lat">Lat: </p>
<p id="lng">Lng: </p>
<p id="address"></p>
</div>

<br />
<div id="map" style="width: 300px; height:300px"></div>
<script type="text/javascript">

function locateAddress() {
    var address = document.getElementById('locate_address').value;
    geocoder = new GClientGeocoder();

    geocoder.getLatLng (
        address,
        function (point) {
            if (!point) {
                alert(address + " not found!");
            }
            else {
                map.setCenter(point, 13);
                var marker = new GMarker (point);
                map.addOverlay (marker);
                marker.openInfoWindowHtml(address);
                document.getElementById('address').innerHTML = address;
                document.getElementById('lat').innerHTML += point.y;
                document.getElementById('lng').innerHTML += point.x;
            }
        })

}



var map = new GMap2(document.getElementById("map"));
map.setCenter(new GLatLng(39.1700149, -86.5148133), 13);

map.setMapType(G_NORMAL_MAP);



</script>







</body>
</html>

1 个答案:

答案 0 :(得分:3)

如果你真的想使用v2 API

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Basic Map</title>


<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=true_or_false&amp;key=ABQIAAAAKgJa4e_5XNFRJJF7MsI1eRQN1RsnicNCcG4bidjiZE76b9vSTBTCbiFNb-LGPKHeiBuV_2yfnWNSTA" type="text/javascript">
</script>



</head>

<body>


Input address Here: <input type="text" name="locate_address" id="locate_address" />
<input type="button" name="locate_address" value="Locate Address" onclick="locateAddress()" />
<div>
<p id="lat">Lat: </p>
<p id="lng">Lng: </p>
<p id="address"></p>
</div>

<br />
<div id="map" style="width: 300px; height:300px"></div>
<script type="text/javascript">

function locateAddress() {
    var address = document.getElementById('locate_address').value;
    geocoder = new GClientGeocoder();


     geocoder.getLocations(address, getFormattedAddress)

}

function getFormattedAddress(response){
console.log(response)
     if (!response || response.Status.code != 200) {
    alert("\"" + address + "\" not found");
  } else {
    //get itemised address
        street = response.Placemark[0].AddressDetails.Country.AdministrativeArea.Locality.Thoroughfare.ThoroughfareName;
        city = response.Placemark[0].AddressDetails.Country.AdministrativeArea.AdministrativeAreaName
    locality = response.Placemark[0].AddressDetails.Country.AdministrativeArea.Locality.LocalityName;
    postcode = response.Placemark[0].AddressDetails.Country.AdministrativeArea.Locality.PostalCode.PostalCodeNumber;
    country = response.Placemark[0].AddressDetails.Country.CountryName;
    alert(street + " " + locality + " " +postcode + " " +country + " "+city)


    place = response.Placemark[0];
    point = new GLatLng(place.Point.coordinates[1],
                        place.Point.coordinates[0]);
    marker = new GMarker(point);
    map.addOverlay(marker);
    marker.openInfoWindowHtml(place.address + '<br>' + 
      '<b>Country code:</b> ' + place.AddressDetails.Country.CountryNameCode);
  }
}

var map = new GMap2(document.getElementById("map"));
map.setCenter(new GLatLng(39.1700149, -86.5148133), 13);

map.setMapType(G_NORMAL_MAP);



</script>







</body>
</html>

for v3见 http://code.google.com/apis/maps/documentation/javascript/services.html#GeocodingResponses