使用
nearbySearch
var service = new google.maps.places.PlacesService(map);
var radius = 100;
service.nearbySearch({
location: new google.maps.LatLng(55.93366899999999, 12.258698299999992),
radius: radius,
type: "store"
}, callback);
返回一个结果:
包豪斯·希勒德
(注:无
type:"store"
proof of concept fiddle
var infowindow;
var map;
function initialize() {
map = new google.maps.Map(
document.getElementById("map_canvas"), {
center: new google.maps.LatLng(55.93366899999999, 12.258698299999992),
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
infowindow = new google.maps.InfoWindow();
var service = new google.maps.places.PlacesService(map);
var radius = 100;
var circle = new google.maps.Circle({
map: map,
center: map.getCenter(),
radius: radius
});
map.fitBounds(circle.getBounds());
service.nearbySearch({
location: map.getCenter(),
radius: radius,
type: "store"
}, callback);
}
function callback(results, status) {
if (status === google.maps.places.PlacesServiceStatus.OK) {
console.log(results.length + " results");
for (var i = 0; i < results.length; i++) {
createMarker(results[i]);
}
} else console.log("status=" + status);
}
function createMarker(place) {
var placeLoc = place.geometry.location;
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(place.name + "<br>placeId:" + place.place_id);
infowindow.open(map, this);
});
google.maps.event.trigger(marker, 'click');
}
google.maps.event.addDomListener(window, "load", initialize);
html,
body,
#map_canvas {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js?libraries=places"></script>
<div id="map_canvas"></div>