Domanda

I am trying to apply Singapore postal code in goggle street map. I am passing postal code in location variable but when I change postcode, map location can’t change. Don’t know where I am wrong. Please help. Here is some post code of Singapore: 189969, 189927 Here is my code

<!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>
È stato utile?

Soluzione

Modify your code so that map will be a google.maps.Map-object.

When you want to show the streetView of the map( inside the callback of geocoder.geocode()for example) , you must:

  1. set the position of the streetview
  2. set the visibility of the streetview

Example:

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

Demo: http://jsfiddle.net/doktormolle/7J4G6/

There are more options to control the streetview,( e.g. I would suggest to check if a streetview is available for the given location), but the both lines above are the minimal requirements.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top