[英]Google Places API getDetails formatted_phone_number returns undefined
I have created this bin to get the details of a place - formatted address and formatted phone number. 我已创建此bin以获取地点的详细信息 - 格式化的地址和格式化的电话号码。
However, the formatted Phone number shows undefined for the places. 但是,格式化的电话号码显示未定义的地点。
However it does show up phone number for places like "Franklin D. Roosevelt Four Freedoms Park, 1 FDR Four Freedoms Park, Roosevelt Island, NY 10044, USA" 然而它确实出现了像“Franklin D. Roosevelt Four Freedoms Park,1 FDR Four Freedoms Park,Roosevelt Island,NY 10044,USA”这样的地方的电话号码。
But, most of the other places are returning undefined. 但是,大多数其他地方都未定义。
For example this place, https://www.google.co.in/maps/place/iStorage+Limited/@51.533075,-0.3119974,17z/data=!3m1!4b1!4m5!3m4!1s0x4876126b1e57f67d:0x4c6d4388c42b1f06!8m2!3d51.5330717!4d-0.3098087 shows phone number in the map. 例如,这个地方, https://www.google.co.in/maps/place/iStorage+Limited/@51.533075,-0.3119974,17z/data=!3m1!4b1!4m5!3m4!1s0x4876126b1e57f67d:0x4c6d4388c42b1f06!8m2! 3d51.5330717!4d-0.3098087在地图中显示电话号码。
But, searching for in the jsbin page, "iStorage Limited 13 Alperton Ln, Perivale, Greenford, Middlesex UB6 8DH, UK" does show that the phone number is undefined. 但是,在jsbin页面中搜索“iStorage Limited 13 Alperton Ln,Perivale,Greenford,Middlesex UB6 8DH,UK”确实显示电话号码未定义。
Am I doing something wrong here in the code ? 我在代码中做错了吗? Or is it because there is no phone number for the other places?
或者是因为其他地方没有电话号码?
Here is the JSBin Link 这是JSBin链接
Any help is greatly appreciated. 任何帮助是极大的赞赏。
Once you have the place id ( ChIJffZXHmsSdkgRBh8rxIhDbUw
for the example in your question), you can do a place details request. 获得地点ID(
ChIJffZXHmsSdkgRBh8rxIhDbUw
中的示例为ChIJffZXHmsSdkgRBh8rxIhDbUw
)后,您可以执行地点详细信息请求。
Make sure you include the Places library in your API call: https://maps.googleapis.com/maps/api/js?libraries=places
确保在API调用中包含Places库:
https://maps.googleapis.com/maps/api/js?libraries=places
: https://maps.googleapis.com/maps/api/js?libraries=places
library https://maps.googleapis.com/maps/api/js?libraries=places
places
function initialize() {
var map = new google.maps.Map(document.getElementById('map-canvas'), {
center: new google.maps.LatLng(0, 0),
zoom: 15
});
var service = new google.maps.places.PlacesService(map);
service.getDetails({
placeId: 'ChIJffZXHmsSdkgRBh8rxIhDbUw'
}, function(place, status) {
if (status === google.maps.places.PlacesServiceStatus.OK) {
// Create marker
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location
});
// Center map on place location
map.setCenter(place.geometry.location);
// Get DIV element to display opening hours
var phoneNumberDiv = document.getElementById("phone-number");
// Create DIV element and append to opening_hours_div
var content = document.createElement('div');
content.innerHTML = 'Formatted phone number: ' + place.formatted_phone_number + '<br>';
content.innerHTML += 'International phone number: ' + place.international_phone_number;
phoneNumberDiv.appendChild(content);
}
});
}
initialize();
Have a look at the below working fiddle: 看看下面的工作小提琴:
I was using the https://ubilabs.github.io/geocomplete/ to get the place_id but, this does not seem to get the correct place_id 我使用https://ubilabs.github.io/geocomplete/来获取place_id但是,这似乎没有得到正确的place_id
I tried https://google-developers.appspot.com/maps/documentation/javascript/examples/full/places-placeid-finder and this does work for me. 我尝试了https://google-developers.appspot.com/maps/documentation/javascript/examples/full/places-placeid-finder ,这对我有用。
So, when trying to get the place details its important to get the correct autocomplete and get the correct place_id 因此,在尝试获取位置详细信息时,重要的是获得正确的自动完成并获取正确的place_id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.