这将产生汽车,如果需要,也可在两个文本箱中获取lat子和长it。 最初的中心是固定的。
<head>
<script type="text/javascript" src=" https://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?libraries=places&sensor=false"></script>
<script type="text/javascript">
var map;
var geocoder;
geocoder = new google.maps.Geocoder();
function initialize() {
var pyrmont = new google.maps.LatLng(-33.8665433,151.1956316);
map = new google.maps.Map(document.getElementById( map ), {
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: pyrmont,
zoom: 15
});
var input = document.getElementById( searchTextField );
var options = {
bounds: pyrmont,
//types: [ establishment ] dont mention it we need to get both bussiness andaddress
};
autocomplete = new google.maps.places.Autocomplete(input, options);
}
$(document).ready(function() {
$("#find").click(function(){
var address = document.getElementById("searchTextField").value;
geocoder.geocode( { address : address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var test = results[0].geometry.location;
var latlang=String(test);
latlang = latlang.substring(0, latlang.length-1);
latlang= latlang.substr(1);
var latlan = latlang.split(",");
var lat = latlan[0];
var lon = latlan[1];
$("#latitude").val(lat);
$("#longitude").val(lon);
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>
<body onload="initialize();">
<label> Type address</label>
<input type="text" id="searchTextField" style="width:500px;" />
<div id="map" style="width:500px;height:500px;"></div>
<input type="text" id="latitude"/>
<input type="text" id="longitude"/>
<input type="button" id="find" value="Find"/>
</body>