2016-09-16 4 views
0

マップのレンダリングに問題があります。マップ上に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最初のパラメータは関数でなければならないよう

+0

あなたはすべての関係のないコードを削除しようとするとちょうど2つのマーカーでマップを作成しましたか?あなたのコードにはたくさんのことがありますが、それを少し絞ることができます。 –

+1

なぜ 'setTimeout( 'geocodeAddress("' +場所[nextAddress] + ''、theNext) '、遅延); '?これはちょうど悪く見える! 'geocodeAddress'は遅れて実行されるのではなく、' setTimeout'の評価時に実行されます –

+0

ええ、それは問題でした。文字列で囲まれている理由は分かりません:)ありがとうそれ ! – Robson

答えて

1
setTimeout('geocodeAddress("'+locations[nextAddress]+'",theN‌​ext)', delay); 

geocodeAddress機能は、exectuedされません。 geocodeAddressが予想遅延で実行されることはありませんので、それにも関わらず

は、

setTimeout(geocodeAddress(locations[nextAddress], theN‌​ext), delay); 

はまだ、格好良いではありませんが、右 setTimeoutが評価するときには、最初のパラメータです。 3秒の遅れで試してみると、私の言いたいことが分かります。

0

私はthe posted codeでJavaScriptのエラーが表示されます。Uncaught ReferenceError: geocodeAddress is not defined

すべてのコードがinit関数にローカルです。 setTimeout構文を使用する場合:

var timeoutID = window.setTimeout(code [、delay]);

コード

オプションの構文は、あなたの代わりにタイマーが満了したときにコンパイルして実行される機能、の文字列を含めることができます。この構文は、eval()をセキュリティリスクとして使用するのと同じ理由で推奨されません。

そのコードはグローバルコンテキストで実行されます。

proof of concept fiddle (map styling removed as not relevant to the issue)

コードスニペット:

google.maps.event.addDomListener(window, 'load', init); 
 
// global variables 
 
var nextAddress = 0; 
 
var delay = 100; 
 
var locations = ['London, United Kingdom', 'Liverpool, United Kingdom']; 
 
var bounds = new google.maps.LatLngBounds(); 
 
var geocoder = new google.maps.Geocoder(); 
 
var infowindow = new google.maps.InfoWindow(); 
 
var map; 
 

 
function init() { 
 
    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 
 
    }; 
 

 
    // 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 
 
    map = new google.maps.Map(mapElement, mapOptions); 
 
    theNext(); 
 
} 
 

 
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); 
 
} 
 

 
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 theNext() { 
 
    if (nextAddress < locations.length) { 
 
    setTimeout('geocodeAddress("' + locations[nextAddress] + '",theNext)', delay); 
 
    nextAddress++; 
 
    } else { 
 
    map.fitBounds(bounds); 
 
    } 
 
}
html, 
 
body, 
 
#map { 
 
    height: 100%; 
 
    width: 100%; 
 
    margin: 0px; 
 
    padding: 0px 
 
}
<script src="https://maps.googleapis.com/maps/api/js"></script> 
 
<div id="map"></div>

関連する問題