先给出如何使用google map的代码:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>google地址解析</title>
<script type="text/javas cript" src="http://maps.google.com/maps/api/js?sensor=false&language=zh-CN"></script>
<script type="text/javas cript">
var geocoder;
var map;
function initialize() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(39.970981, 116.314108);
var myOptions = {
zoom: 15,
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;
if (geocoder) {
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
alert(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
}
</script>
</head>
<body on load="initialize()">
<div id="map_canvas" style="width: 320px; height: 480px;"></div>
<div>
<input id="address" type="textbox" value="中国人民大学">
<input type="button" value="Encode" on click="codeAddress()">
</div>
</body>
</html>
这个代码可以进行地址解析,google map 跟baidu map都可以进行地址解析和反向解析。
看下效果:(在ie中运行,其他浏览器可能不支持ActiveX)
再看下,baidu map的代码:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>baidu 地址解析</title>
<script type="text/javas cript" src=" http://api.map.baidu.com/api?key=458d39374361da27e548367a735831ba&v=1.0&services=true" ></script>
</head>
<body>
<div style="position:absolute;width:320px;height:480px;top:50;left:0;border:1px solid gray;overflow-y:hidden;" id="container"></div>
<input id="text_" type="text" value="中国人民大学"/>
<input type="button" value="encode" on Click="searchByStationName();">
</body>
<script>
var map = new BMap.Map("container");
map.centerAndZoom(new BMap.Point(116.314108,39.970981), 15);
var localSearch= new BMap.LocalSearch (map,{
renderOptions: {
pageCapacity: 8,
autoViewport: true,
selectFirstResult: false
}
});
localSearch.enableAutoViewport();
function searchByStationName()
{
var keyword = document.getElementById("text_").value;
localSearch.setSearchCompleteCallback(function(searchResult){
var poi = searchResult.getPoi(0);
alert(poi.point.lng+" "+poi.point.lat);
map.centerAndZoom(poi.point, 8);
});
localSearch.search(keyword);
}
</script>
</html>
看下效果:
以上在IE6、IE7中不可以,其他的都可以。
done!good luck!