嗨,我正在尝试在单击图形时让一些图钉显示在谷歌地图上。我将我需要的所有数据存储在跨度标签上的数据位置元素中:
<span class="showOnMap" data-location="{"lat":"51.4894805","lng":"-0.2942533000000367","name":"Brentford Towers","addr":"Green Dragon Lane\r\nBrentford TW8 0DF","pinType":"loc"}"> <img width="32" height="32" src="/images/Icons/map-icon.png">
</span>
我的点击功能是:
$('.showOnMap').click(function(){
//alert('click');
var myData = $(this).data('location');
console.log(myData);
createMarkerWithInfo(myData);
//addToPanel(panelData);
})
创建标记的功能给我带来了一个问题,当我提醒它或尝试使用它来创建标记时,传递给它的数据显示为未定义,但它确实显示在控制台中,但从我一直以来不应该信任读取控制台,因为它会显示当前状态的数据,即在实际调用控制台时可能没有定义它,
我不明白为什么它未定义为 json 数据在 dom 中(它实际上由 php 生成)所以在 dom 中定义,我可以理解如果我正在执行 Ajax 调用,为什么它可能会出现未定义。
function createMarkerWithInfo(myData){
//console.log(myData);
//coords = myData.latlng.split(',');
alert(myData + 'pinType: '+ myData.pinType + ' lat: '+ myData.lat + ' lng: ' +myData.lng);
myLatLng = new google.maps.LatLng(myData.lat, myData.lng);
if(myData.pinType == 'loc'){
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
title: myData.name,
icon: locimage,
shadow: locshadow,
shape: locshape
});
}
if(myData.pinType == 'ub'){
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
title: myData.name,
icon: ubimage,
shadow: ubshadow,
shape: locshape
});
}
//alert('lat: '+myData.lat+','+'lng: '+myData.lng);
// Wrapping the event listener inside an anonymous function
// that we immediately invoke and passes the variable i to.
(function(myData, marker) {
// Creating the event listener. It now has access to the values of
// myData and marker as they were during its creation
google.maps.event.addListener(marker, 'click', function() {
//create thecontent for the infowindow
var content = createContent(myData);
infowindow.setContent(content);
infowindow.open(map, marker);
});
})(myData, marker);
markersArray.push(marker);
markersPosArray.push(myLatLng);
bounds.extend(myLatLng);
map.fitBounds(bounds);
zoomMarkers();
}
http://2012.reelfilmlocations.co.uk/Modules/builder_areaLocs.php 是我正在处理的页面,奇怪的是我有一个类似的页面(使用 php 包含而不是独立页面),它具有相同的功能并且它作品,我看了两者,也看不出差异是什么导致它未定义) http://2012.reelfilmlocations.co.uk/locations-london-west/