Javascript 获取格式整齐的地址:Latlng->;谷歌地图地址
我正在制作一个页面,它利用Google Maps geocoder对象查找用户输入的地址。我能够找到lat/lng点,但我想格式化地址(清除它),这样我就可以将lat、lng和地址存储在MySQL数据库中的3个单独字段中。这是我目前的代码: 在安全性方面也可以随意批评当前的代码,我对GMAPSAPI完全陌生,对js不是很熟练Javascript 获取格式整齐的地址:Latlng->;谷歌地图地址,javascript,mysql,google-maps,google-maps-api-3,Javascript,Mysql,Google Maps,Google Maps Api 3,我正在制作一个页面,它利用Google Maps geocoder对象查找用户输入的地址。我能够找到lat/lng点,但我想格式化地址(清除它),这样我就可以将lat、lng和地址存储在MySQL数据库中的3个单独字段中。这是我目前的代码: 在安全性方面也可以随意批评当前的代码,我对GMAPSAPI完全陌生,对js不是很熟练 基本地图 在此处输入地址: lat: 液化天然气: 函数locateAddress(){ var address=document.getElementById('l
基本地图
在此处输入地址:
lat:
液化天然气:
函数locateAddress(){
var address=document.getElementById('locate_address')。值;
geocoder=新的GClientGeocoder();
geocoder.getLatLng(
地址:,
功能(点){
如果(!点){
警报(地址+“未找到!”);
}
否则{
地图设定中心(点13);
var标记=新的GMarker(点);
map.addOverlay(标记);
marker.openInfoWindowHtml(地址);
document.getElementById('address')。innerHTML=地址;
document.getElementById('lat').innerHTML+=point.y;
document.getElementById('lng').innerHTML+=point.x;
}
})
}
VarMap=新的GMap2(document.getElementById(“map”);
赛特中心地图(新格拉特林(39.1700149,-86.5148133),13);
setMapType(G_NORMAL_map);
如果您真的想使用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&v=2&sensor=true_or_false&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>
基本地图
在此处输入地址:
lat:
液化天然气:
函数locateAddress(){
var address=document.getElementById('locate_address')。值;
geocoder=新的GClientGeocoder();
geocoder.getLocations(地址,getFormattedAddress)
}
函数getFormattedAddress(响应){
console.log(响应)
如果(!response | | response.Status.code!=200){
警报(“\”“+地址+”\“未找到”);
}否则{
//获取详细地址
street=response.Placemark[0]。AddressDetails.Country.AdministrativeArea.Location.Throughway.ThroughwayName;
city=response.Placemark[0]。AddressDetails.Country.AdministrativeArea.AdministrativeArea Name
Location=response.Placemark[0]。AddressDetails.Country.AdministrativeArea.Location.LocalityName;
postcode=response.Placemark[0]。AddressDetails.Country.AdministrativeArea.Location.PostalCode.PostalCode.PostalCodeNumber;
country=response.Placemark[0]。AddressDetails.country.CountryName;
警报(街道+地区+邮政编码+国家+城市)
地点=响应。地点标记[0];
点=新玻璃(位置点坐标[1],
地点点坐标[0];
标记器=新的GMarker(点);
添加覆盖图(标记);
marker.openInfoWindowHtml(place.address+'
'+
“国家/地区代码:”+place.AddressDetails.Country.CountryNameCode);
}
}
VarMap=新的GMap2(document.getElementById(“map”);
赛特中心地图(新格拉特林(39.1700149,-86.5148133),13);
setMapType(G_NORMAL_map);
有关v3,请参阅
使用谷歌地图Api的v3。您正在使用的版本2已被弃用。在版本3中,我将如何执行此操作?
<!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&v=2&sensor=true_or_false&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>