2016-12-14 65 views
-1

我在谷歌地圖上創建了一個多邊形,當我點擊多邊形時想要彈出一個infoWindow。代號爲如下:Google Map API InfoWindow點擊事件沒有返回latlng

google.maps.event.addListener(polygon, 'click', showVolume); 
polygon_infoWindow = new google.maps.InfoWindow; 

在showvolume你的功能,

function showVolume(event){ 
    var polygon_Name = this.Name; 
    var volume = this.Volume; 
    var pt = event.latlng; 
    var contentString = '<b>Name: </b>' + polygon_Name + '<br>' + '<b>Volume: </b>' + volume + '<br>' + '<b>Clicked Location: </b>' + pt; 

    polygon_infoWindow.setContent(contentString); 
    polygon_infoWindow.open(map); 
    polygon_infoWindow.setPosition(pt); 
} 

函數返回屬於多邊形的正確信息。但是它返回event.latlng爲undefined。我不知道什麼是錯的。

+0

我得到一個JavaScript錯誤與張貼的co de:'Uncaught ReferenceError:polygon_infoWindow is not defined' – geocodezip

+0

我的歉意,我的定義是錯誤的,因爲我改變了我的代碼以保存機密信息。現在它被定義爲polygon_infoWindow。 – simonww

回答

0

event.latlng不存在,JavaScript是大小寫敏感的,屬性爲event.latLng

proof of concept fiddle

代碼片段:

var geocoder; 
 
var map; 
 
var polygon_infoWindow; 
 

 
function initialize() { 
 
    map = new google.maps.Map(
 
    document.getElementById("map_canvas"), { 
 
     center: new google.maps.LatLng(37.4419, -122.1419), 
 
     zoom: 13, 
 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
 
    }); 
 
    var polygon = new google.maps.Polygon({ 
 
    map: map, 
 
    Name: "Fred", 
 
    Volume: 1000, 
 
    path: [{ 
 
     lat: 37.4419, 
 
     lng: -122.1419 
 
    }, { 
 
     lat: 37.42, 
 
     lng: -122.16 
 
    }, { 
 
     lat: 37.4419, 
 
     lng: -122.1819 
 
    }] 
 
    }); 
 
    google.maps.event.addListener(polygon, 'click', showVolume); 
 
    polygon_infoWindow = new google.maps.InfoWindow; 
 
} 
 
google.maps.event.addDomListener(window, "load", initialize); 
 

 
function showVolume(event) { 
 
    var polygon_Name = this.Name; 
 
    var volume = this.Volume; 
 
    var pt = event.latLng; 
 
    var contentString = '<b>Name: </b>' + polygon_Name + '<br>' + '<b>Volume: </b>' + volume + '<br>' + '<b>Clicked Location: </b>' + pt; 
 

 
    polygon_infoWindow.setContent(contentString); 
 
    polygon_infoWindow.setPosition(pt); 
 
    polygon_infoWindow.open(map); 
 

 
}
html, 
 
body, 
 
#map_canvas { 
 
    height: 100%; 
 
    width: 100%; 
 
    margin: 0px; 
 
    padding: 0px 
 
}
<script src="https://maps.googleapis.com/maps/api/js"></script> 
 
<div id="map_canvas"></div>

+0

這工作!謝謝! latLng而不是LatLng。但是,當我瀏覽Google文檔[鏈接](https://developers.google.com/maps/documentation/javascript/infowindows)時,我發現它說的是LatLng。 – simonww

相關問題