通过地图经纬度得到地理信息
function initialize() {
map = new GMap2(document.getElementById("map"));
map.setCenter(new GLatLng(39.90822, 116.4055), 13);
map.addControl(new GLargeMapControl);
GEvent.addListener(map, "click", getAddress);
geocoder = new GClientGeocoder();
}
function getAddress(marker, point,msg) {//通过经纬度得到一个地理位置
if (point != null) {
geocoder.getLocations(point, function(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);
marker.openInfoWindowHtml(
'<b>经纬度:</b>' + response.name + '<br/>' +
'<b>地理位置:</b>' + place.address + '<br>'+
'<b>警报信息:</b>'+msg);
}
});
}
主要是GClientGeocoder这个类的getLocations这个方法通过一个GLatLng得到response信息,然后在进行解析就可以了!
{
"name": "1600 Amphitheatre Parkway, Mountain View, CA, USA",
"Status": {
"code": 200,
"request": "geocode"
},
"Placemark": [
{
"address": "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
"AddressDetails": {
"Country": {
"CountryNameCode": "US",
"AdministrativeArea": {
"AdministrativeAreaName": "CA",
"SubAdministrativeArea": {
"SubAdministrativeAreaName": "Santa Clara",
"Locality": {
"LocalityName": "Mountain View",
"Thoroughfare": {
"ThoroughfareName": "1600 Amphitheatre Pkwy"
},
"PostalCode": {
"PostalCodeNumber": "94043"
}
}
}
}
},
"Accuracy": 8
},
"Point": {
"coordinates": [-122.083739, 37.423021, 0]
}
}
]
}
function initialize() {
map = new GMap2(document.getElementById("map"));
map.setCenter(new GLatLng(39.90822, 116.4055), 13);
map.addControl(new GLargeMapControl);
GEvent.addListener(map, "click", getAddress);
geocoder = new GClientGeocoder();
}
function getAddress(marker, point,msg) {//通过经纬度得到一个地理位置
if (point != null) {
geocoder.getLocations(point, function(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);
marker.openInfoWindowHtml(
'<b>经纬度:</b>' + response.name + '<br/>' +
'<b>地理位置:</b>' + place.address + '<br>'+
'<b>警报信息:</b>'+msg);
}
});
}
主要是GClientGeocoder这个类的getLocations这个方法通过一个GLatLng得到response信息,然后在进行解析就可以了!
{
"name": "1600 Amphitheatre Parkway, Mountain View, CA, USA",
"Status": {
"code": 200,
"request": "geocode"
},
"Placemark": [
{
"address": "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
"AddressDetails": {
"Country": {
"CountryNameCode": "US",
"AdministrativeArea": {
"AdministrativeAreaName": "CA",
"SubAdministrativeArea": {
"SubAdministrativeAreaName": "Santa Clara",
"Locality": {
"LocalityName": "Mountain View",
"Thoroughfare": {
"ThoroughfareName": "1600 Amphitheatre Pkwy"
},
"PostalCode": {
"PostalCodeNumber": "94043"
}
}
}
}
},
"Accuracy": 8
},
"Point": {
"coordinates": [-122.083739, 37.423021, 0]
}
}
]
}
本文详细介绍了如何通过JavaScript结合GMap2和GClientGeocoder类,从给定的经纬度获取并解析地理信息的过程,包括设置地图中心、添加控制项以及通过点击事件获取地理位置。
7398

被折叠的 条评论
为什么被折叠?



