我做了一個簡單的GoogleMap,它響應script.php?q=canada
並顯示一張地圖。簡單谷歌地圖和地理編碼
一個問題是,它總是將地圖加載中心var latlng = new google.maps.LatLng(34.052234,-118.243685);
一秒鐘,然後加載正確的地圖。
我的代碼有什麼問題?
<!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><?=$_GET['q']?></title>
<link href="http://code.google.com/apis/maps/documentation/javascript/examples/default.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="//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.052234,-118.243685);
var address = '<?=$_GET['q']?>';
var myOptions = {
zoom: 10,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
geocoder.geocode({ 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
// autozoom
map.fitBounds(results[0].geometry.viewport);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location,
title: 'by Quick Maps',
clickable: false
});
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
</script>
</head>
<body onload="initialize()">
<div id="map_canvas"></div>
</body>
</html>
Sidequestion: 如何與地圖上的一個簡單的信息替換alert("Geocode was not successful for the following reason: " + status);
?
'結果[0] .geometry.location'是google.maps.LatLng物件。你應該使用'results [0] .geometry.location.lat()'和'results [0] .geometry.location.lng()'而不是'.Pa'和'.Qa',因爲這些變量可以改變並擁有)。 –
謝謝,我解決了答案:) –