マップのレンダリングに問題があります。マップ上に2つのマーカーを動的に表示したい。ジオコード2つのアドレスと作成マップjs googlemaps api v3
地図はレンダリングされていますが、マーカーは表示されません。
はここでマップのための私のコードです:
<script type="text/javascript">
// When the window has finished loading create our google map below
google.maps.event.addDomListener(window, 'load', init);
function init() {
var delay = 100;
var infowindow = new google.maps.InfoWindow();
var mapOptions = {
// How zoomed in you want the map to start at (always required)
zoom: 11,
// The latitude and longitude to center the map (always required)
center: new google.maps.LatLng(51.507351, -0.127758), // New York
// How you would like to style the map.
// This is where you would paste any style found on Snazzy Maps.
styles: [{
"featureType": "water",
"elementType": "geometry",
"stylers": [{"color": "#e9e9e9"}, {"lightness": 17}]
}, {
"featureType": "landscape",
"elementType": "geometry",
"stylers": [{"color": "#f5f5f5"}, {"lightness": 20}]
}, {
"featureType": "road.highway",
"elementType": "geometry.fill",
"stylers": [{"color": "#ffffff"}, {"lightness": 17}]
}, {
"featureType": "road.highway",
"elementType": "geometry.stroke",
"stylers": [{"color": "#ffffff"}, {"lightness": 29}, {"weight": 0.2}]
}, {
"featureType": "road.arterial",
"elementType": "geometry",
"stylers": [{"color": "#ffffff"}, {"lightness": 18}]
}, {
"featureType": "road.local",
"elementType": "geometry",
"stylers": [{"color": "#ffffff"}, {"lightness": 16}]
}, {
"featureType": "poi",
"elementType": "geometry",
"stylers": [{"color": "#f5f5f5"}, {"lightness": 21}]
}, {
"featureType": "poi.park",
"elementType": "geometry",
"stylers": [{"color": "#dedede"}, {"lightness": 21}]
}, {
"elementType": "labels.text.stroke",
"stylers": [{"visibility": "on"}, {"color": "#ffffff"}, {"lightness": 16}]
}, {
"elementType": "labels.text.fill",
"stylers": [{"saturation": 36}, {"color": "#333333"}, {"lightness": 40}]
}, {
"elementType": "labels.icon",
"stylers": [{"visibility": "off"}]
}, {
"featureType": "transit",
"elementType": "geometry",
"stylers": [{"color": "#f2f2f2"}, {"lightness": 19}]
}, {
"featureType": "administrative",
"elementType": "geometry.fill",
"stylers": [{"color": "#fefefe"}, {"lightness": 20}]
}, {
"featureType": "administrative",
"elementType": "geometry.stroke",
"stylers": [{"color": "#fefefe"}, {"lightness": 17}, {"weight": 1.2}]
}]
};
// Get the HTML DOM element that will contain your map
// We are using a div with id="map" seen below in the <body>
var mapElement = document.getElementById('map');
// Create the Google Map using our element and options defined above
var map = new google.maps.Map(mapElement, mapOptions);
var bounds = new google.maps.LatLngBounds();
var geocoder = new google.maps.Geocoder();
function geocodeAddress(address, next) {
geocoder.geocode({address:address}, function (results,status)
{
if (status == google.maps.GeocoderStatus.OK) {
var p = results[0].geometry.location;
var lat=p.lat();
var lng=p.lng();
createMarker(address,lat,lng);
}
else {
if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT) {
nextAddress--;
delay++;
} else {
}
}
next();
}
);
}
function createMarker(add,lat,lng) {
var contentString = add;
var marker = new google.maps.Marker({
position: new google.maps.LatLng(lat,lng),
map: map,
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(contentString);
infowindow.open(map,marker);
});
bounds.extend(marker.position);
}
var locations = ['London, United Kingdom', 'Liverpool, United Kingdom'];
var nextAddress = 0;
function theNext() {
if (nextAddress < locations.length) {
setTimeout('geocodeAddress("'+locations[nextAddress]+'",theNext)', delay);
nextAddress++;
} else {
map.fitBounds(bounds);
}
}
theNext();
}
</script>
私が正しくこれらのマーカーを表示することができますどのように?私はいくつかの間違いを犯した提案やフィードバックに感謝します。 setTimeout
最初のパラメータは関数でなければならないよう
あなたはすべての関係のないコードを削除しようとするとちょうど2つのマーカーでマップを作成しましたか?あなたのコードにはたくさんのことがありますが、それを少し絞ることができます。 –
なぜ 'setTimeout( 'geocodeAddress("' +場所[nextAddress] + ''、theNext) '、遅延); '?これはちょうど悪く見える! 'geocodeAddress'は遅れて実行されるのではなく、' setTimeout'の評価時に実行されます –
ええ、それは問題でした。文字列で囲まれている理由は分かりません:)ありがとうそれ ! – Robson