Javascript Google Places API中每个位置类型的单独标记
我有以下代码显示学校和清真寺附近的一些位置Javascript Google Places API中每个位置类型的单独标记,javascript,google-maps,Javascript,Google Maps,我有以下代码显示学校和清真寺附近的一些位置 <script src="https://maps.googleapis.com/maps/api/js?sensor=true&libraries=places"></script> <script> var map; var infowindow; function initialize() { var pyrmont = new google.ma
<script src="https://maps.googleapis.com/maps/api/js?sensor=true&libraries=places"></script>
<script>
var map;
var infowindow;
function initialize() {
var pyrmont = new google.maps.LatLng(<?php echo $lat;?>, <?php echo $lng;?>);
map = new google.maps.Map(document.getElementById('map1'), {
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: pyrmont,
zoom: 15
});
var request = {
location: pyrmont,
radius: 500,
types: ['school' , 'mosque']
};
infowindow = new google.maps.InfoWindow();
var service = new google.maps.places.PlacesService(map);
service.search(request, callback);
}
function callback(results, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
for (var i = 0; i < results.length; i++) {
createMarker(results[i]);
}
}
}
base_Icon = "<?php echo JURI::base();?>components/com_properties/includes/img/school.png";
shadow_Icon = "<?php echo JURI::base();?>components/com_properties/includes/img/house-shadow.png";
function createMarker(place) {
var placeLoc = place.geometry.location;
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location,
shadow: shadow_Icon,
icon: base_Icon
});
google.maps.event.addListener(marker, 'mouseover', function() {
infowindow.setContent(place.name);
infowindow.open(map, this);
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
<div id="map1" style=" width:<?php echo $params->get('WidthMap','100%');?>; height:<?php echo $params->get('HeightMap','300px');?>;margin:0px;padding:0px;"></div>
var映射;
var信息窗口;
函数初始化(){
var pyrmont=newgoogle.maps.LatLng(,);
map=new google.maps.map(document.getElementById('map1'){
mapTypeId:google.maps.mapTypeId.ROADMAP,
中心:皮尔蒙特,
缩放:15
});
var请求={
地点:皮尔蒙特,
半径:500,
类型:[“学校”、“清真寺”]
};
infowindow=new google.maps.infowindow();
var service=newgoogle.maps.places.PlacesService(地图);
服务搜索(请求、回调);
}
函数回调(结果、状态){
if(status==google.maps.places.PlacesServiceStatus.OK){
对于(var i=0;i 在createMarker函数中,可以引用place.types
——一个类型数组。注意:有些地方会有不止一种类型。您应该检查该数组是否包含“学校”或“清真寺”,并根据需要设置用于该标记的图标
base_Icon_school = "<?php echo JURI::base();?>components/com_properties/includes/img/school.png";
base_Icon_mosque = "<?php echo JURI::base();?>components/com_properties/includes/img/mosque.png";
shadow_Icon = "<?php echo JURI::base();?>components/com_properties/includes/img/house-shadow.png";
function createMarker(place) {
var placeLoc = place.geometry.location;
var marker;
var icon_to_use;
if (place.types.indexOf('school') != -1) {
icon_to_use = base_Icon_school;
} else if (place.types.indexOf('mosque') != -1) {
icon_to_use = base_Icon_mosque;
}
marker = new google.maps.Marker({
map: map,
position: place.geometry.location,
shadow: shadow_Icon,
icon: icon_to_use
});
google.maps.event.addListener(marker, 'mouseover', function() {
infowindow.setContent(place.name);
infowindow.open(map, this);
});
}
base\u Icon\u school=“components/com\u properties/includes/img/school.png”;
base_Icon_moskie=“components/com_properties/includes/img/moskie.png”;
shadow\u Icon=“components/com\u properties/includes/img/house shadow.png”;
函数createMarker(place){
var placeLoc=place.geometry.location;
var标记;
变量图标的使用;
if(place.types.indexOf('school')!=-1){
icon\u to\u use=基本icon\u学校;
}else if(place.types.indexOf('mosque')!=-1){
icon_to_use=base_icon_;
}
marker=新的google.maps.marker({
地图:地图,
位置:place.geometry.location,
阴影:阴影图标,
图标:要使用的图标
});
google.maps.event.addListener(标记'mouseover',函数(){
infowindow.setContent(place.name);
打开(地图,这个);
});
}
请参阅:我如何检查数组是否包含学校或清真寺?请您写几行,告诉我它将如何工作。现在我不知道它将如何工作。看看这个问题的答案是否适用于你。谢谢@邓肯代码工作完美。。!