javascript - 有没有更好的方法让谷歌地图工作?
问题描述
我正在使用 cakephp,这个项目是关于庄园的。我正在尝试将带有图片的庄园放在谷歌地图上。我当前的代码就像
<script>
var map;
var infoWindow;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: 36.591258, lng: 136.624976},
zoom: 9,
styles:[
{
"featureType": "landscape",
"elementType": "all",
"stylers": [
{
"color": "#e4e4e4"
}
]
},
{
"featureType": "poi",
"elementType": "all",
"stylers": [
{
"visibility": "off"
}
]
},
{
"featureType": "road",
"elementType": "all",
"stylers": [
{
"saturation": -100
},
{
"lightness": 45
}
]
},
{
"featureType": "road.highway",
"elementType": "all",
"stylers": [
{
"visibility": "simplified"
}
]
},
{
"featureType": "road.highway",
"elementType": "geometry.fill",
"stylers": [
{
"color": "#fc9700"
}
]
},
{
"featureType": "road.arterial",
"elementType": "geometry.fill",
"stylers": [
{
"color": "#c5c5c5"
}
]
},
{
"featureType": "road.arterial",
"elementType": "labels.icon",
"stylers": [
{
"visibility": "off"
}
]
},
{
"featureType": "road.local",
"elementType": "all",
"stylers": [
{
"visibility": "on"
}
]
},
{
"featureType": "transit",
"elementType": "all",
"stylers": [
{
"visibility": "off"
}
]
},
{
"featureType": "water",
"elementType": "all",
"stylers": [
{
"color": "#999797"
},
{
"visibility": "on"
}
]
}
],
});
<?php foreach ($rooms as $k => $room): ?>
var marker = new google.maps.Marker({
position: {lat: <?= h($room['estate_latitude']) ?>, lng: <?= h($room['estate_longitude']) ?>},
map: map,
icon: new google.maps.MarkerImage(
'<?= $this->Url->build('/') ?>img/ico_pin.svg',
new google.maps.Size(30, 30),
new google.maps.Point(0, 0),
new google.maps.Point(21, 21)
),
});
infoWindow<?= $k ?> = new google.maps.InfoWindow({
content: '<div class="area_wall_balloon"></div><div class="img_arrow"></div>'
});
marker.addListener('click', function() {
infoWindow<?= $k ?>.open(map, marker);
});
<?php endforeach; ?>
var mapStyle = [ {
"stylers": [ {
"saturation": -100,
} ],
} ];
}
</script>
在此处输入图像描述 我要解决的问题是,当我单击 2 时,会显示 1 的窗口:( 我添加了屏幕截图。如果您能给我建议,我将不胜感激。
解决方案
你完全混合了 JavaScript 和 PHP。正如我所见,您正在为多个标记创建多个事件。意味着如果有 1000 个标记,那么为什么要使用 1000 个事件触发器。为此,仅使用一个呈现标记细节的事件。
推荐阅读
- python - Django 1.8 条件表达式返回空查询集
- c# - 如何捕获 Ignite Thin .net 客户端异常
- android - Kotlin Channels 在片段之间发送事件
- c - C: unsigned long int 的行为不符合预期
- react-native - React Native Maps - 标注不显示内部图像
- biztalk - BizTalk - 推广多个值,订阅其中一个
- python - Django 教程应用程序,第 2 部分。为什么缺少 - 在 makemigrations 命令后添加字段 Question to Choice?
- flutter - 颤振:RaisedButton 比它的 SizedBox 大
- javascript - 为什么时刻转换此日期不正确?
- batch-file - 如何在批处理中正确使用重命名(ren)