gpt4 book ai didi

google-maps - 带有自定义图标的 Google Maps API v3 兴趣点

转载 作者:行者123 更新时间:2023-12-04 06:07:59 28 4
gpt4 key购买 nike

我有一个页面在我给定区域的学校、教堂和公园中拉取,但我想用 3 个不同的图标来设计 3 个 POI。我搜索了谷歌甚至所有的文档,但找不到答案。

var map;
var infowindow;

function initialize() {

// Center of Map
var centerLatlng = new google.maps.LatLng(29.745376, -95.380125);

// Marker Icons Declaration
var icon = new google.maps.MarkerImage("smLinks-twitter.png", null, null, new google.maps.Point(41,47));

// Map Options
var myOptions = {
zoom: 16,
center: centerLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP,
icons: icon
};

// Draw the map
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

// Marker Icons Implementation
markers = new google.maps.Marker({
position: centerLatlng,
map: map,
title: 'Center of Map',
icon: icon
});

// Services: Places
var request = {
location: centerLatlng,
radius: 800,
types: ["school", "church", "park"]
};
infowindow = new google.maps.InfoWindow();
var service = new google.maps.places.PlacesService(map);
service.search(request, callback);

} // function initialize()

function callback(results, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
for (var i = 0; i < results.length; i++) {
createMarker(results[i]);
}
}
}

function createMarker(place) {
var placeLoc = place.geometry.location;
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location,
icon: icon
});
google.maps.event.addListener(marker, 'mouseover', function() {
infowindow.setContent(place.name + '<br/>' + place.vicinity + '<br/><img src="' + place.icon + '">');
infowindow.open(map, this);
});
}

最佳答案

请看我的又快又脏Demo .这个想法是使用 place.types数组来确定您要添加到 map 的地点类型。我简单地为该数组的第一项分配了一个标记(通常为 2 或 3 项长),可能类似于:

["school", "establishment"]

在某些情况下,“大学”在“学校”之前,因此使用“大学”图标。您是否希望改进将类型与图标匹配的方式,即优先考虑学校或大学?也许遍历数组寻找正确的类型。一个地方 (general_contractor) 不在我的图标列表中,所以默认的 pin 标记放在那里。如果您选中 iconType,则可以使用“默认”图标事实上有或没有所需的类型。我会把这些细节留给你=)

这是我用于图标的来源: https://sites.google.com/site/gmapsdevelopment/
function createMarker(place) {
var placeLoc = place.geometry.location;

var iconType = {};
iconType['school'] = "http://maps.google.com/mapfiles/kml/pal2/icon2.png";
iconType['church'] = "http://maps.google.com/mapfiles/kml/pal2/icon11.png";
iconType['park'] = "http://maps.google.com/mapfiles/kml/pal2/icon12.png";
iconType['university'] = "http://maps.google.com/mapfiles/kml/pal2/icon14.png";

var marker = new google.maps.Marker({
map: map,
position: place.geometry.location,
icon: iconType[place.types[0]]
});

google.maps.event.addListener(marker, 'mouseover', function() {
infowindow.setContent(place.name + '<br/>' + place.vicinity + '<br/><img src="' + place.icon + '">');
infowindow.open(map, this);
});
}

或者,使用 switch 语句:
function createMarker(place) {
var placeLoc = place.geometry.location;

var iconUrl;
switch (place.types[0]) {
case 'school':
iconUrl = "http://maps.google.com/mapfiles/kml/pal2/icon2.png";
break;
case 'church':
iconUrl = "http://maps.google.com/mapfiles/kml/pal2/icon11.png";
break;
case 'park':
iconUrl = "http://maps.google.com/mapfiles/kml/pal2/icon12.png";
break;
case 'university':
iconUrl = "http://maps.google.com/mapfiles/kml/pal2/icon14.png";
break;
default:
iconUrl = "http://maps.google.com/mapfiles/kml/pal4/icon39.png";
}

var marker = new google.maps.Marker({
map: map,
position: place.geometry.location,
icon: iconUrl
});

google.maps.event.addListener(marker, 'mouseover', function() {
infowindow.setContent(place.name + '<br/>' + place.vicinity + '<br/><img src="' + place.icon + '">');
infowindow.open(map, this);
});
}

关于google-maps - 带有自定义图标的 Google Maps API v3 兴趣点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11269231/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com