很抱歉为这样一个新手问题寻求帮助。但是使用maps api非常困惑。它没有为类型国家/地区显示任何内容,有时它只显示与类型相同的结果:store for type:establishment。
<!DOCTYPE html>
<html>
<head>
<style>
#map {
height: 500px;
}
</style>
<script>
var nlat, nlong;
nlat= 19.0759837; //default
nlong = 72.87765590000004; // default
getLocation();
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
alert("Geolocation is not supported by this browser.");
}
}
function showPosition(position) {
nlat = position.coords.latitude;
nlong = position.coords.longitude;
}
var map;
var infowindow;
var plcsearch = "";
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: nlat, lng: nlong},
zoom: 10
});
infowindow = new google.maps.InfoWindow();
var service = new google.maps.places.PlacesService(map);
service.nearbySearch({
location: {lat: nlat, lng: nlong},
radius: 5000000,
type: ['country']
}, callback);
}
function callback(results, status) {
if (status === google.maps.places.PlacesServiceStatus.OK) {
for (var i = 0; i < results.length; i++) {
createMarker(results[i]);
plcsearch = plcsearch + "<br>" + results[i].name;
}
}
var x = document.getElementById("plcrad");
x.innerHTML = plcsearch;
}
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);
infowindow.open(map, this);
});
}
</script>
</head>
<body>
<div id="map"></div>
<div id="plcrad">List of Countries</div>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCEBvD7N9NkG4_1xAFPpPH6FTJLIk-_2TY
&libraries=places&callback=initMap" async defer></script>
</body>
</html>
此外,您也可以在此处看到同样的问题:https://developers.google.com/maps/documentation/javascript/examples/place-search 请帮帮我。提前谢谢。
答案 0 :(得分:0)
为了支持Dr.Molle的评论,国家/地区不是搜索请求的有效类型。
放置类型
以下是一些受支持的类型列表,可用于过滤地点搜索和添加地点。
关于建立或商店的相同结果的其他问题,以下是搜索的示例回复:
"place_id" : "ChIJ5xQ7szeuEmsRs6Kj7YFZE9k",
"scope" : "GOOGLE",
"reference" : "CnRvAAAA22k1PAGyDxAgHZk6ErHh_h_mLUK_8XNFLvixPJHXRbCzg-gw1ZxdqUwA_8EseDuEZKolBs82orIQH4m6-afDZV9VcpggokHD9x7HdMi9TnJDmGb9Bdh8f-Od4DK0fASNBL7Me3CsAWkUMWhlNQNYExIQ05W7VbxDTQe2Kh9TiL840hoUZfiO0q2HgDHSUyRdvTQx5Rs2SBU",
"types" : [ "food", "establishment" ],
"vicinity" : "48 Pirrama Rd, Pyrmont"
如响应所示,某个地点可以有多种类型,具体取决于它们的添加方式。为了更好地理解某些搜索请求返回相同结果的方式或原因,我们可以仔细检查响应并查看每个地方的类型标记。
供参考,您可以进一步查看types supported in place search and addition,types not supported in the type filter of a place search和search Responses。