【问题标题】:Google map street view with postcode带有邮政编码的谷歌地图街景
【发布时间】:2013-01-12 06:10:47
【问题描述】:

我正在尝试在 goggle 街道地图中应用新加坡邮政编码。我在位置变量中传递邮政编码,但是当我更改邮政编码时,地图位置无法更改。不知道我哪里错了。请帮忙。这是新加坡的一些邮政编码: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>

【问题讨论】:

  • 地图是 StreetViewPanorama 对象。它没有 setCenter 方法。您需要重新考虑代码的设计。您是否想要一张带有可打开该位置街景的标记的地图(邮政编码?)?或者您是否希望每个邮政编码有多个 StreetView 对象。邮政编码的街景有什么用?
  • 是的,我想要一张带有标记的地图,可以打开该位置的街景(邮政编码)。

标签: google-maps postal-code


【解决方案1】:

修改您的代码,使map 成为google.maps.Map 对象。

当你想显示地图的街景时(例如在geocoder.geocode()的回调中),你必须:

  1. 设置街景位置
  2. 设置街景的可见性

例子:

map.streetView.setPosition(results[0].geometry.location);
map.streetView.setVisible(true);

演示:http://jsfiddle.net/doktormolle/7J4G6/

有更多选项可以控制街景,(例如,我建议检查给定位置是否有街景),但以上两条线都是最低要求。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-25
    • 1970-01-01
    • 1970-01-01
    • 2014-04-04
    • 2015-10-09
    • 2010-10-25
    相关资源
    最近更新 更多