参考地址:http://code.google.com/intl/zh-CN/apis/maps/documentation/javascript/examples/index.html
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Google Maps JavaScript API v3 Example: Geocoding Simple</title>
<link href="http://code.google.com/apis/maps/documentation/javascript/examples/default.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var geocoder;
var map;
function initialize() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(-34.397, 150.644);
var myOptions = {
zoom: 8,
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;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(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 onload="initialize()">
<div>
<input id="address" type="textbox" value="Sydney, NSW">
<input type="button" value="Geocode" onclick="codeAddress()">
</div>
<div id="map_canvas" style="height:90%;top:30px"></div>
</body>
</html>
自己:
var g_weidu_min = 31.1235;
var g_weidu_max = 31.28;
var g_jingdu_min = 121.36;
var g_jingdu_max = 121.58;
this.geocode = function(address)
{
alert("123");
if (geocoder) {
var southwest = new google.maps.LatLng(31.1235, 121.36);
var northeast = new google.maps.LatLng(31.28, 121.58);
var bounds = new google.maps.LatLngBounds(southwest, northeast); //限定查询范围
geocoder.geocode( { 'address': address, 'bounds': bounds}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK)
{
if(results[0].geometry.location.lat() > g_weidu_min && results[0].geometry.location.lat() < g_weidu_max && results[0].geometry.location.lng() > g_jingdu_min && results[0].geometry.location.lng() < g_jingdu_max)
{
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map, //定义在map中显示标记。
position: results[0].geometry.location //定义标记的位置
});
markersArray.push(marker);
}
}
});
}
};
完