[英]Google Map API, can't get name of a place
I'm trying to do a little project using the Google Map API and my problem is that I do not find how to get the name of a place on the Map when I click on it.我正在尝试使用 Google Map API 做一个小项目,但我的问题是当我点击它时,我找不到如何在地图上获取地点的名称。 By place I mean the things represented as a circle with a picture inside (museum, restaurant, monuments, etc.) Like this
我说的地方是指用圆圈表示的东西,里面有图片(博物馆、餐厅、纪念碑等)像这样
In this picture, the default action of the map is to open an info-windows when I click on it, with the name of the place, that's exactly what I want BUT I have no solution to "extract" the name (To use it somewhere else) because this is a default reaction...在这张图片中,地图的默认操作是当我点击它时打开一个信息窗口,带有地点名称,这正是我想要的,但我没有解决方案来“提取”名称(使用它其他地方)因为这是默认反应......
I've tried to get it with the following code :我尝试使用以下代码获取它:
var service = new google.maps.places.PlacesService(map);
google.maps.event.addListener(map, 'click', function(event) {
getAddress(event.latLng);
});
function getAddress(latLng) {
geocoder.geocode( {'latLng': latLng},
function(results, status) {
if(status === google.maps.GeocoderStatus.OK) {
var arrayLength = results.length;
for (var i = 0; i < arrayLength; i++) {
var request = {
placeId: results[i].place_id_id
};
service.getDetails(request, callback);
}
}
});
}
function callback(place, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
console.log(place);
}
}
But the only precised thing returned is the address (in the example : "37 Phillip St, Sydney NSW 2000, Australie") but never the name of the place.但返回的唯一精确内容是地址(在示例中:“37 Phillip St, Sydney NSW 2000, Australie”)但从来没有地名。
And I've really found nothing on Google, seems really weird...而且我真的在谷歌上什么也没找到,看起来真的很奇怪......
Thank you for your help !感谢您的帮助 ! :)
:)
As per the code you have provided, you are using the PlaceDetails
from which you can actually already get the data you need.根据您提供的代码,您正在使用
PlaceDetails
,您实际上已经可以从中获取所需的数据。 As per the Places Details Results docs :根据地点详细信息结果文档:
A successful getDetails() call returns a
PlaceResult
object.成功的 getDetails() 调用会返回一个
PlaceResult
对象。
Wherein PlaceResult
object pretty much has the info that you might need.其中
PlaceResult
对象几乎包含您可能需要的信息。 Haven't tested it out, but it has the address_components
property that has a structure like so (referred to Geocoder response sample on this one):尚未对其进行测试,但它具有
address_components
属性,该属性具有如下结构(在此参考Geocoder 响应示例):
"address_components" : [
{
"long_name" : "1600",
"short_name" : "1600",
"types" : [ "street_number" ]
},
{
"long_name" : "Amphitheatre Pkwy",
"short_name" : "Amphitheatre Pkwy",
"types" : [ "route" ]
},
{
"long_name" : "Mountain View",
"short_name" : "Mountain View",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Santa Clara County",
"short_name" : "Santa Clara County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "California",
"short_name" : "CA",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "94043",
"short_name" : "94043",
"types" : [ "postal_code" ]
}
]
and also the name
property which is simply described in the docs as:以及在文档中简单描述为的
name
属性:
The place's name.
地名。
Just tweak around the results of the PlaceDetails request and I'm pretty sure you'll find it.只需调整 PlaceDetails 请求的结果,我很确定您会找到它。 Hope this helps.
希望这可以帮助。 Good luck.
祝你好运。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.