我想在这里做什么。当我按下标记时。它应该将我发送到该ID的页面。它发送我到ID的页面。但是,我单击哪个标记并始终访问相同ID的页面并不重要。侦听器无法正常工作...我想...
这是完整的地图:
var estates = <?php echo json_encode($estates);?>;
function initMap()
{
var options =
{
zoom : 6,
center : {lat:34.652500, lng:135.506302}
};
var map = new google.maps.Map(document.getElementById('map'), options);
@foreach ($estates as $est)
var marker = new google.maps.Marker({
map: map,
icon: 'imgs/marker.png',
url: "/pages/{{$est->id}}",
label: {
text: estates.data[0].price.substring(0, 5),
color: "#fff",
},
position: {
lat: {{$est->lat}},
lng: {{$est->lng}}
}
});
google.maps.event.addListener(marker, 'click', function () {
window.location.href = marker.url;
});
@endforeach
}
有解决此问题的主意吗?
答案 0 :(得分:4)
请尝试:
google.maps.event.addListener(marker, 'click', function () {
window.location.href = this.url;
});
答案 1 :(得分:0)
似乎window.location.href = marker.url;
中[i]
中marker
缺少
答案 2 :(得分:-1)
我认为是因为它会覆盖您的标记,因此只会保存您的最后一个标记... 您是否尝试添加标记并点击循环?
@foreach ($locations as $est)
var marker = new google.maps.Marker({
map: map,
icon: 'imgs/marker.png',
url: "/pages/{{$loc->id}}",
label: {
text: locations.data[0].price.substring(0, 5),
color: "#fff",
},
position: {
lat: {{$loc->lat}},
lng: {{$loc->lng}}
}
});
google.maps.event.addListener(marker, 'click', function () {
window.location.href = marker.url;
});
@endforeach
或第二个选项先在数组中添加标记,然后循环播放...
var arrMarker = [];
var i = 0;
@foreach ($locations as $est)
var marker = new google.maps.Marker({
map: map,
icon: 'imgs/marker.png',
url: "/pages/{{$loc->id}}",
label: {
text: locations.data[0].price.substring(0, 5),
color: "#fff",
},
position: {
lat: {{$loc->lat}},
lng: {{$loc->lng}}
}
});
arrMarker[i] = marker;
i++;
@endforeach
for(var i = 0; i < pages.length; i++){
google.maps.event.addListener(arrMarker[i], 'click', function () {
window.location.href = arrMarker[i].url;
});
}
修改第三个选项。将标记推入数组。
var markers = [];
@foreach ($locations as $est)
var marker = new google.maps.Marker({
map: map,
icon: 'imgs/marker.png',
url: "/pages/{{$loc->id}}",
label: {
text: locations.data[0].price.substring(0, 5),
color: "#fff",
},
position: {
lat: {{$loc->lat}},
lng: {{$loc->lng}}
}
});
markers.push(marker)
google.maps.event.addListener(marker, 'click', function () {
window.location.href = marker.url;
});
@endforeach