我仍然是Google Maps API和JS的新手,所以这可能(可能) 有一个简单的答案。
我现在正在返回我所在位置的某个半径范围内的所有地方 设定但我想更具体,例如只有植物苗圃,加油站和健身房,只显示那些标记。
对于长代码块感到抱歉,如果您愿意,这里有一个JSBin RFC 7519
提前致谢。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Locator</title>
<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBdodiLO598_RD8_NYXK7nBKNA9Fhx_uBQ&libraries=places,geometry&.js"></script>
<script
src="https://code.jquery.com/jquery-3.1.1.js"
integrity="sha256-16cdPddA6VdVInumRGo6IbivbERE8p7CQR3HzTBuELA="
crossorigin="anonymous"></script>
</head>
<body>
<input id="findMe" type="button" value="find closest place">
<div id="map-canvas" style="height:500px;"></div>
</body>
</body>
</html>
JS:
<script>
jQuery(function($) {
var $overlay = $('.overlay'),
resize = true,
map;
var service;
var marker = [];
var pos;
var infowindow;
var placeLoc
function initialize() {
var mapOptions = {
zoom: 15
};
map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
// Try HTML5 geolocation
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var pos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
$('#findMe').data('pos', pos);
var request = {
location: pos,
radius: 1000,
};
infowindow = new google.maps.InfoWindow();
var service = new google.maps.places.PlacesService(map);
service.nearbySearch(request, callback);
infowindow = new google.maps.InfoWindow({
map: map,
position: pos,
content: 'You Are Here'
});
map.setCenter(pos);
}, function() {
handleNoGeolocation(true);
});
} else {
// Browser doesn't support Geolocation
handleNoGeolocation(false);
}
function callback(results, status) {
var markers = [];
if (status == google.maps.places.PlacesServiceStatus.OK) {
for (var i = 0; i < results.length; i++) {
markers.push(createMarker(results[i]));
}
}
$('#findMe').data('markers', markers);
}
}
function createMarker(place) {
placeLoc = place.geometry.location;
var marker = new google.maps.Marker({
map: map,
type: ['store'],
position: place.geometry.location,
icon: {
path: google.maps.SymbolPath.CIRCLE,
scale: 8,
fillColor: '00a14b',
fillOpacity: 0.3,
fillStroke: '00a14b',
strokeWeight: 4,
strokeOpacity: 0.7
},
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(place.name);
infowindow.open(map, this);
});
return marker;
}
function handleNoGeolocation(errorFlag) {
if (errorFlag) {
var content = 'Error: The Geolocation service failed.';
} else {
var content = 'Error: Your browser doesn\'t support geolocation.';
}
var options = {
map: map,
position: new google.maps.LatLng(60, 105),
content: content
};
var infowindow = new google.maps.InfoWindow(options);
map.setCenter(options.position);
}
google.maps.event.addDomListener(window, 'load', initialize);
$('#show').click(function() {
$overlay.show();
if (resize) {
google.maps.event.trigger(map, 'resize');
resize = false;
}
});
$('.overlay-bg').click(function() {
$overlay.hide();
});
$("#findMe").click(function() {
var pos = $(this).data('pos'),
markers = $(this).data('markers'),
closest;
if (!pos || !markers) {
alert('pos or markers not set yet');
return;
}
$.each(markers, function() {
var distance = google.maps.geometry.spherical.computeDistanceBetween(this.getPosition(), pos);
if (!closest || closest.distance > distance) {
closest = {
marker: this,
distance: distance
}
}
});
if (closest) {
google.maps.event.trigger(closest.marker, 'click')
}
});
});
</script>
答案 0 :(得分:1)
非常简单。您需要使用名为types to filter的新属性传递编辑请求所需的类型数组。 Ex类型:[&#39;银行&#39;,&#39;健身房&#39;]
下面的代码块,我将附上你的JS Bin的修改版本
可以从下面的链接中找到地点类型
Google Places API文档 - developers.google.com/maps/documentation/javascript/places#place_search_requests
var request = {
location: pos,
radius: 1000,
types: ['bank', 'gym']
};
infowindow = new google.maps.InfoWindow();
var service = new google.maps.places.PlacesService(map);
service.nearbySearch(request, callback);