google map在弹出层中展示经纬度
效果图如下:
此处用的弹出层是我播客中前面提到的方法,这里我就不再提出来了。直接说google地址的反向解析问题:
需要的js地址:http://maps.google.com/maps?file=api&v=2&,也可以直接用此连接。
以下代码就可以实现:
var map;
var geocoder;
var address;
var webLongitude;
var webLatitude;
function initialize() {
map = new GMap2(document.getElementById("map_canvas"));
map.setCenter(new GLatLng(40.730885,-73.997383), 15);
map.setUIToDefault();
GEvent.addListener(map, "click", getAddress);
geocoder = new GClientGeocoder();
}
function getAddress(overlay, latlng) {
if (latlng != null) {
address = latlng;
geocoder.getLocations(latlng, showAddress);
}
}
function showAddress(response) {
map.clearOverlays();
if (!response || response.Status.code != 200) {
alert("Status Code:" + response.Status.code);
} else {
place = response.Placemark[0];
point = new GLatLng(place.Point.coordinates[1],
place.Point.coordinates[0]);
marker = new GMarker(point);
map.addOverlay(marker);
//全局付值, 用于页面需要它的地方获取到
webLongitude = place.Point.coordinates[1];
webLatitude = place.Point.coordinates[0];
marker.openInfoWindowHtml(
'<b>latlng:</b>' + place.Point.coordinates[1] + "," + place.Point.coordinates[0] + '<br>' +
'<b>Address:</b>' + place.address + '<br>' +
'<b>Accuracy:</b>' + place.AddressDetails.Accuracy + '<br>' +
'<b>Country code:</b> ' + place.AddressDetails.Country.CountryNameCode );
}
}