以下代码有效。 - 它在预定位置500米范围内的地图上显示“商店”。 - 它在地图上显示带有商店详细信息的标记。 - 它确实显示了地图列表,但只显示地名而不是详细信息(我想了解详情)。
如果我想输出上面的HTML / Javascript列表而不是显示地图,怎么会找到如何做到这一点?
Google Places API,重点介绍如何输出到地图,但我不想仅在地图上输出商店信息。如果您有任何线索,请告诉我。我只需要建议。
<!DOCTYPE html>
<html>
<head>
<title>Place searches</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<style>
html, body, #map-canvas {
height: 100%;
margin: 0px;
padding: 0px
}
</style>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=places"></script>
<script>
var map, placesList;
var infowindow;
var service; //declare globally
function initialize() {
var pyrmont = new google.maps.LatLng(40.062664, -83.069101);
map = new google.maps.Map(document.getElementById('map-canvas'), {
center: pyrmont,
zoom: 15
});
var request = {
location: pyrmont,
radius: 500,
types: ['store']
};
infowindow = new google.maps.InfoWindow();
placesList = document.getElementById('places');
service = new google.maps.places.PlacesService(map);
service.nearbySearch(request, callback);
service.getDetails(request, callback);
}
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
});
var request = { placeId: place.place_id };
service.getDetails(request, function(details, status) {
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(details.name + "<br />" + details.formatted_address +"<br />" + details.website + "<br />" + details.rating + "<br />" + details.formatted_phone_number);
infowindow.open(map, this);
});
});
placesList.innerHTML += '<li>' + place.name + '</li>';
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
<style>
#results {
font-family: Arial, Helvetica, sans-serif;
position: absolute;
right: 5px;
top: 50%;
margin-top: -195px;
height: 380px;
width: 500px;
padding: 5px;
z-index: 5;
border: 1px solid #999;
background: #fff;
}
h2 {
font-size: 22px;
margin: 0 0 5px 0;
}
ul {
list-style-type: none;
padding: 0;
margin: 0;
height: 321px;
width: 500px;
overflow-y: scroll;
}
li {
background-color: #f1f1f1;
padding: 10px;
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
}
li:nth-child(odd) {
background-color: #fcfcfc;
}
#more {
width: 100%;
margin: 5px 0 0 0;
}
</style>
</head>
<body>
<div id="map-canvas"></div>
<div id="results">
<h2>Results</h2>
<ul id="places"></ul>
<button id="more">More results</button>
</div>
</body>
</html>
答案 0 :(得分:1)
我看到你已经填写了一个html的地方列表,为什么还不够?
关于地方详细信息,我尝试在相关标记上设置监听器,而不是尝试获取每个检索到的地方的详细信息。如果有人点击该标记,那么我将启动详细信息请求并相应地打开信息窗
function createMarker(place) {
var placeLoc = place.geometry.location;
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location
});
var request = { placeId: place.place_id };
google.maps.event.addListener(marker, 'click', function() {
service.getDetails(request, function(details, status) {
console.log(details,status);
infowindow.setContent(details.name + "<br />" + details.formatted_address +"<br />" + details.website + "<br />" + details.rating + "<br />" + details.formatted_phone_number);
infowindow.open(map, marker);
});
});
placesList.innerHTML += '<li>' + place.name + '</li>';
}
反过来会导致“OVER QUERY LIMIT”错误,因为你将同时向getDetails发出500个请求。
更新:here's a brief example,使用jQuery检测html列表上的点击并触发getDetails请求。