Google Maps Application Developing —— Search Address
<script type="text/javascript">
(function(){
var map, geocoder, marker, infowindow;
window.onload = function(){
var mapOptions = {
zoom:14,
center:new google.maps.LatLng(39.9, 116.4),
mapTypeId:google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map'), mapOptions);
var form = document.getElementById('addressForm');
form.onsubmit = function(){
var address = document.getElementById('address').value;
getCoordinates(address);
return false;
}
}
function getCoordinates(address){
if(!geocoder){
geocoder = new google.maps.Geocoder();
}
var geocoderRequest = {address:address}
geocoder.geocode(geocoderRequest, function(results, status){
if(status == google.maps.GeocoderStatus.OK){
map.setCenter(results[0].geometry.location);
if(!marker){
marker = new google.maps.Marker({map:map});
}
marker.setPosition(results[0].geometry.location);
if(!infowindow){
infowindow = new google.maps.InfoWindow();
}
var content = '<strong>' + results[0].formatted_address +
'</strong><br/>';
content += '纬度: ' + results[0].geometry.location.lat() + '<br/>';
content += '经度: ' + results[0].geometry.location.lng();
infowindow.setContent(content);
infowindow.open(map, marker);
}
});
}
})();
作者:Create Chen
出处:http://technology.cnblogs.com
说明:文章为作者平时里的思考和练习,可能有不当之处,请博客园的园友们多提宝贵意见。
本作品采用知识共享署名-非商业性使用-相同方式共享 2.5 中国大陆许可协议进行许可。
This is a demo
浙公网安备 33010602011771号