<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml"> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> <title>Google 地图 JavaScript API 示例: 简单地址解析</title> <script src="http://ditu.google.cn/maps?file=api&v=2.x&key=ABQIAAAAzr2EBOXUKnm_jVnk0OJI7xSosDVG8KKPE1-m51RBrvYughuyMxQ-i1QfUnH94QxWIa6N4U6MouMmBA&hl=zh-CN" type="text/javascript"></script> <script type="text/javascript"> var map; var geocoder; var address; function initialize() { if (GBrowserIsCompatible()) { var options = { listingTypes : "kmlonly" }; map = new GMap2(document.getElementById("map_canvas"),{ size: new GSize(1000,800) } ); map.setCenter(new GLatLng(22.815687,108.324834), 12); //这里点击显示地址 // map.addControl(new GSmallMapControl()); GEvent.addListener(map, "click", getAddress); geocoder = new GClientGeocoder(); var customUI = map.getDefaultUI(); customUI.maptypes.hybrid = true; map.setUI(customUI); map.enableGoogleBar(); } }
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.Place[0]; point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]); er = new GMarker(point); map.addOverlay(er); er.openInfoWindowHtml( '<div style="font-size:13px;">' + '<b>你现在所点击的地址:</b><br/>' + place.address + '<br/><br/>' + '<b>坐标:</b>' + place.Point.coordinates[1] + ' ' + place.Point.coordinates[0] + '<b>准确度:</b>' + place.AddressDetails.Accuracy + ' ' + '<b>国家代码:</b> ' + place.AddressDetails.Country.CountryNameCode + '</div>'); } }
//地理位置解析*********************************************************************************************** function $(id){return document.getElementById(id);} function addAddressToMap(response) { map.clearOverlays(); if (!response || response.Status.code != 200) { alert("不能解析这个地址"); } else { place = response.Place[0]; point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]); er = new GMarker(point); map.addOverlay(er); er.openInfoWindowHtml(getGeocodeHtml(response)); } } function getGeocodeHtml(response){ var html = "<div style='font-size:13px'>"; html += "<b>搜索目标:</b>"+response.name; // html += "<br/><b>结果状态:</b>"+response.Status.code; if(response.Place){ for (var i = 0, place; place = response.Place[i]; i++) { html += "<br/><b>地址:</b>"+place.address; // if(place.AddressDetails.Country)html += "<br/><b>国家代码:</b>"+place.AddressDetails.Country.CountryNameCode; // html += "<br/><b>准确度:</b>"+place.AddressDetails.Accuracy; html += "<br/><b>坐标:</b>"+place.Point.coordinates[1]+place.Point.coordinates[0]; } } html += "</div>"; return html; } function showLocation() { var address = $("q").value; geocoder.getLocations(address, addAddressToMap); } function findLocation(address) { $("q").value = address; showLocation(); updateURL(); } //地理位置解析***********************************************************************************************
rgtr
呵呵
[/url]
呵呵
[/url]
要免费的话,一般JAVASCRIPT的GOOGLE API就能够实现。楼主最好还是花点功夫研究一下GOOGLE 自定义地图,像这样一个工程的地图,不是一两段代码就可以完成的,还需要配合数据库(坐标位置,餐馆的信息资料等)。
真要那么简单的话,大家都去做口碑网了。
google地图文档参见http://code.google.com/intl/zh-CN/apis/maps/documentation/javascript/basics.html
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Google 地图 JavaScript API 示例: 简单地址解析</title>
<script src="http://ditu.google.cn/maps?file=api&v=2.x&key=ABQIAAAAzr2EBOXUKnm_jVnk0OJI7xSosDVG8KKPE1-m51RBrvYughuyMxQ-i1QfUnH94QxWIa6N4U6MouMmBA&hl=zh-CN" type="text/javascript"></script>
<script type="text/javascript"> var map;
var geocoder;
var address; function initialize() {
if (GBrowserIsCompatible()) {
var options = {
listingTypes : "kmlonly"
};
map = new GMap2(document.getElementById("map_canvas"),{ size: new GSize(1000,800) } );
map.setCenter(new GLatLng(22.815687,108.324834), 12);
//这里点击显示地址
// map.addControl(new GSmallMapControl());
GEvent.addListener(map, "click", getAddress);
geocoder = new GClientGeocoder();
var customUI = map.getDefaultUI();
customUI.maptypes.hybrid = true;
map.setUI(customUI);
map.enableGoogleBar();
}
}
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.Place[0];
point = new GLatLng(place.Point.coordinates[1],
place.Point.coordinates[0]);
er = new GMarker(point);
map.addOverlay(er);
er.openInfoWindowHtml(
'<div style="font-size:13px;">' +
'<b>你现在所点击的地址:</b><br/>' + place.address + '<br/><br/>' +
'<b>坐标:</b>' + place.Point.coordinates[1] + ' ' + place.Point.coordinates[0] +
'<b>准确度:</b>' + place.AddressDetails.Accuracy + ' ' +
'<b>国家代码:</b> ' + place.AddressDetails.Country.CountryNameCode +
'</div>');
}
}
//地理位置解析***********************************************************************************************
function $(id){return document.getElementById(id);}
function addAddressToMap(response) {
map.clearOverlays();
if (!response || response.Status.code != 200) {
alert("不能解析这个地址");
} else {
place = response.Place[0];
point = new GLatLng(place.Point.coordinates[1],
place.Point.coordinates[0]);
er = new GMarker(point);
map.addOverlay(er);
er.openInfoWindowHtml(getGeocodeHtml(response));
}
}
function getGeocodeHtml(response){
var html = "<div style='font-size:13px'>";
html += "<b>搜索目标:</b>"+response.name;
// html += "<br/><b>结果状态:</b>"+response.Status.code;
if(response.Place){
for (var i = 0, place; place = response.Place[i]; i++) {
html += "<br/><b>地址:</b>"+place.address;
// if(place.AddressDetails.Country)html += "<br/><b>国家代码:</b>"+place.AddressDetails.Country.CountryNameCode;
// html += "<br/><b>准确度:</b>"+place.AddressDetails.Accuracy;
html += "<br/><b>坐标:</b>"+place.Point.coordinates[1]+place.Point.coordinates[0];
}
}
html += "</div>";
return html;
}
function showLocation() {
var address = $("q").value;
geocoder.getLocations(address, addAddressToMap);
}
function findLocation(address) {
$("q").value = address;
showLocation();
updateURL();
}
//地理位置解析***********************************************************************************************
</script>
</head> <body onload="initialize()" onunload="GUnload()">
<b>目标地址/坐标:</b>
<input type="text" id="q" value="请输入您要搜索的位置 如: 南宁市 朝阳广场" class="address_input" size="42" />
<input type="button" value="搜索" onclick="showLocation();"/>
<div id="map_canvas" style="width: 80%; height: 80%"></div>
</body>
</html>
这个只是简单的搜索,要做到楼主想要的效果,必须和数据库相结合,或者外存到KML文件加载。
而且GOOGLE也提供了根据地名直接显示位置的API
不过太久没用了,都忘记光了……