2016-11-11 49 views
-1

我目前有一個標記,名爲chicagoMarker,我爲此創建了一個事件偵聽器。每次用戶靠近我的城市時,我都遇到了一個警告框出現問題。以下是我有:帶標記的警報框Google Maps API

<!DOCTYPE html> 
<html> 
    <head> 
    <title>Simple Map</title> 
    <meta name="viewport" content="initial-scale=1.0"> 
    <meta charset="utf-8"> 
    <style> 
     /* Always set the map height explicitly to define the size of the div 
     * element that contains the map. */ 
     #map { 
     height: 100%; 
     } 
     /* Optional: Makes the sample page fill the window. */ 
     html, body { 
     height: 100%; 
     margin: 0; 
     padding: 0; 
     } 
    </style> 
    </head> 
    <body> 
    <div id="map"></div> 
    <script> 
     var map; 
     var score; 
     score = 0; 
     function initMap() { 
     var chicago = {lat: 41.8781, lng: -87.6298}; 
     var indianapolis = {lat: 39.7684, lng: -86.1581}; 
     var oklahomaCity = {lat: 35.4819, lng: -97.5084}; 
     map = new google.maps.Map(document.getElementById('map'), { 
      center: {lat: 0.0, lng: 0.0}, 
      zoom: 1 
}); 
     var chicagoMarker = new google.maps.Marker({ 
      position: chicago, 


}); 

     var oklahomaCityMarker = new google.maps.Marker({ 
      position: oklahomaCity, 
}); 

     var indianapolisMarker = new google.maps.Marker({ 
      position: indianapolis, 

}); 

if (score==0) { 
    chicagoMarker.setMap(map); 
    map.getZoom(); 
    google.maps.event.addListener(chicagoMarker,'bounds_changed',function(){ 
     if (map.getBounds().contains(chicagoMarker)){ 
      alert("Good Job"); 
     } 
     if (zoom == 8 && map.getBounds.contains(chicagoMarker)){ 
      window.alert("You have found city 1!"); 
      score = score + 1; 
     } 
    }) 

}; 

if (score==1) { 
    oklahomaCityMarker.setMap(map) 
} 

if (score==2) { 
    indianapolisMarker.setMap(map) 
} 


chicagoMarker.setVisible(false); 
indianapolisMarker.setVisible(false); 
oklahomaCityMarker.setVisible(false); 
    } 
    </script> 
    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCuvsCAF0gVmwv6AF0SoA3xBjV66RG4r7o&callback=initMap" 
    async defer></script> 
    </body> 
</html> 
+0

能否請您添加整個代碼越大,城市被發現?我們無法僅根據此代碼知道發生了什麼。 – Ionut

回答

0

您不能檢查,如果地圖包含的標記,因爲你錯過了類google.maps.LatLng需要一個經緯度,而不是一個標記作爲參數的 .getPosition()(方法 )

因此改線

if (map.getBounds().contains(chicagoMarker)){ 

to 

if (map.getBounds().contains(chicagoMarker.getPosition())){ 

您也可以使用這樣的

map.addListener('bounds_changed',function(){ 

事件偵聽器添加到地圖(代替google.maps.event.addListener(chicagoMarker ...

我改變了一些東西,使你的腳本的工作,你可以找到的結果在這個片段。現在,當變焦比10

var map; 
 
     var score; 
 
     score = 0; 
 
     function initMap() { 
 
     var chicago = {lat: 41.8781, lng: -87.6298}; 
 
     var indianapolis = {lat: 39.7684, lng: -86.1581}; 
 
     var oklahomaCity = {lat: 35.4819, lng: -97.5084}; 
 
     map = new google.maps.Map(document.getElementById('map'), { 
 
      center: {lat: 0.0, lng: 0.0}, 
 
      zoom: 10 
 
}); 
 
     var chicagoMarker = new google.maps.Marker({ 
 
      position: chicago, 
 

 

 
}); 
 

 
     var oklahomaCityMarker = new google.maps.Marker({ 
 
      position: oklahomaCity, 
 
}); 
 

 
     var indianapolisMarker = new google.maps.Marker({ 
 
      position: indianapolis, 
 

 
}); 
 

 
if (score==0) { 
 
    chicagoMarker.setMap(map); 
 
    
 
    map.addListener('bounds_changed',function(){ 
 
     zoom = map.getZoom(); 
 
     console.log(zoom); 
 
     if (zoom >=10 && map.getBounds().contains(chicagoMarker.getPosition())){ 
 
      window.alert("You have found city 1!"); 
 
      score = score + 1; 
 
     } 
 
    }) 
 

 
}; 
 

 
if (score==1) { 
 
    oklahomaCityMarker.setMap(map) 
 
} 
 

 
if (score==2) { 
 
    indianapolisMarker.setMap(map) 
 
} 
 

 

 
chicagoMarker.setVisible(false); 
 
indianapolisMarker.setVisible(false); 
 
oklahomaCityMarker.setVisible(false); 
 
    }
#map { 
 
     height: 80%; 
 
     } 
 
    
 
     html, body { 
 
     height: 100%; 
 
     margin: 0; 
 
     padding: 0; 
 
     }
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCuvsCAF0gVmwv6AF0SoA3xBjV66RG4r7o&callback=initMap" 
 
    async defer></script> 
 
<div id="map"></div>

+0

當我靠近時,我仍然沒有收到警報。我的事件監聽器有問題嗎? – Nate

+0

好的,我打開了if函數,但仍然沒有運氣。 – Nate

+0

所以我應該添加事件監聽器,在那裏我初始化我的地圖呢? – Nate