Google Places API-地址和电话号码? [英] Google Places API- Address and phone number?
问题描述
我正在尝试通过Google地图创建列表和地图。以下显示制造商和本地商店的列表。现在它只显示商店的名称。我将如何包含地址和电话?我会添加另一个对象规范吗?
I'm trying to create a list and map via Google Places. The following displays makers and a list of local stores. Right now it only shows the name of the store. How would I include Address and Phone? Would I add another object specification?
更新:根据以下链接: Google Places API - 放置详细信息请求undefined
UPDATE: According to this link: Google Places API - Places detail request undefined
应添加place.getDetails()。但是,我的脚本现在只显示一个市场而不显示细节。这是怎么回事?
place.getDetails() should be added. However my script now only shows one market and not details. What is going on?
UPDATE UPDATE:
UPDATE UPDATE:
HTML例子在这里: http://simplecomputerblog.com/searchtest1.html
HTML example here: http://simplecomputerblog.com/searchtest1.html
<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 = { reference: 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>
推荐答案
从文档,您可以轻松获取数字
和<$ c的句柄
From the documentation, you can easily get a handle on the number
and address
of the place(s) you fetch by doing:
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+place.formatted_address+place.formatted_phone_number);
infowindow.open(map, this);
});
placesList.innerHTML += '<li>' + place.name + '</li>';
}
同样来自 docs :
Again from the docs:
InfoWindow的内容可能包含一串文本,HTML代码片段
或一个DOM元素。要设置内容,请在InfoWindowOptions中指定它
或明确地在InfoWindow
上调用setContent()。
The content of the InfoWindow may contain a string of text, a snippet of HTML, or a DOM element. To set the content, either specify it within the InfoWindowOptions or call setContent() on the InfoWindow explicitly.
< 编辑 -
正确地指出 nearbySearch 不会返回 formatted_address
和 formatted_phone_number的nofollow> results
在结果
对象中,因此您必须创建另一个 getdetails
请求你已经加入你的代码。它可能看起来像(未测试):
EDIT-
As rightly pointed out results for nearbySearch
don't return formatted_address
and formatted_phone_number
in the result
object and for that you'll have to make another getdetails
request like you have already incorporated in your code. It might look something like (untested):
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>';
}
重要的是 service
可用于你使用它的函数的范围。所以在所有函数之外声明它,并像下面这样适当地初始化它:
It's important that service
is available to the scope of the functions you use it in. So declare it outside all the functions and initialize it appropriately like so:
var service; //declare globally
function intialize(){
...
service = new google.maps.places.PlacesService(map); //initialize here
...
}
您可以相应地格式化该内容字符串并填充infowindow。希望它能让你从正确的方向开始。
So you can format that content string accordingly and populate the infowindow. Hope it gets you started in the right direction.
这篇关于Google Places API-地址和电话号码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!