我正在尝试将新加坡邮政编码应用于护目镜街道地图。我在位置变量中传递邮政编码,但是当我更改邮政编码时,地图位置无法更改。不知道我哪里错了。请帮忙。以下是新加坡的一些邮政编码:189969,189927 这是我的代码
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title></title>
<script type='text/javascript' src='mootools-core-1.3.2.js'></script>
<link rel="stylesheet" type="text/css" href="/css/normalize.css">
<style type='text/css'>
#map_canvas {
background:#ccc;
height:400px;
width:100%;
}
</style>
<script type='text/javascript'>//<![CDATA[
window.addEvent('load', function() {
var map;
var geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(1.280095,103.850949);
var panoramaOptions = {
position: latlng,
};
map = new google.maps.StreetViewPanorama(document.getElementById('map_canvas'),panoramaOptions);
var locations = ['189969'];
function codeAddress(locations) {
for (i = 0; i < locations.length; i++) {
geocoder.geocode( { 'address': locations[i]}, 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);
}
});
}
}
codeAddress(locations);
});//]]>
</script>
</head>
<body>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<div id="map_canvas">map</div>
</body>
</html>
答案 0 :(得分:3)
修改您的代码,以便map
成为google.maps.Map
- 对象。
当您想要显示地图的streetView时(例如在geocoder.geocode()
的回调中),您必须:
示例:
map.streetView.setPosition(results[0].geometry.location);
map.streetView.setVisible(true);
演示:http://jsfiddle.net/doktormolle/7J4G6/
还有更多选项来控制街景,(例如,我建议检查街道视图是否适用于给定位置),但上述两行都是最低要求。