我是 JavaScript 新手,但是这个问题已经让我发疯了两天,我找不到和我有同样问题的人。
我正在尝试添加一个按钮来隐藏我的地图上的所有标记(最终我想这样做,以便我按类别隐藏标记,但暂时隐藏所有标记就可以了)我正在使用来自谷歌的代码开发者网站。
这是我的代码
var map;
var service;
var infowindow;
function initialize() {
var pyrmont = new google.maps.LatLng(49.2755189938682, -123.1185996613159);
map = new google.maps.Map(document.getElementById('map'), {
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: pyrmont,
zoom: 17
});
var request = {
location: pyrmont,
radius: '500',
types: ['atm','bus_station','parking']
};
infowindow = new google.maps.InfoWindow();
service = new google.maps.places.PlacesService(map);
service.nearbySearch(request, callback);
TestMarker();
}
function callback(results, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
for (var i = 0; i < results.length; i++) {
var place = results[i];
createMarker(results[i]);
}
}
}
function TestMarker() {
CentralPark = new google.maps.LatLng(49.2755189938682, -123.1185996613159);
addMarker(CentralPark);
}
function addMarker(location) {
marker = new google.maps.Marker({
position: location,
map: map,
icon: "http://maps.google.com/mapfiles/ms/micons/POI.png",
title: 'Sues Party, Idaburn Salon'
});
}
function createMarker(place) {
var iconType = {};
iconType['atm'] = "http://maps.google.com/mapfiles/ms/micons/dollar.png";
iconType['bus_station'] = "http://maps.google.com/mapfiles/ms/micons/bus.png";
//iconType['restaurant'] = "http://maps.google.com/mapfiles/ms/micons/restaurant.png";
iconType['parking'] = "http://maps.google.com/mapfiles/ms/micons/parkinglot.png";
var placeLoc = place.geometry.location;
var marker = new google.maps.Marker({
map: map,
icon: iconType[place.types[0]],
position: place.geometry.location
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(place.name);
infowindow.open(map, this);
});
}
google.maps.event.addDomListener(window, 'load', initialize);
我添加了一个按钮来调用 a function
,但我认为问题在于从数组中获取记录。真的卡住了,非常感谢任何帮助。甚至建议我需要做什么才能让它发挥作用
詹姆士