2014-11-17 30 views
-1

我正尝试通过Google地图创建列表和地图。以下显示制造商和本地商店的列表。现在它只显示商店的名称。我将如何包含地址和电话?我会添加另一个对象规范吗?Google Places API-地址和电话号码?

更新:根据这个链接:Google Places API - Places detail request undefined

place.getDetails()应增加。但是,我的脚本现在只显示一个市场而不显示细节。到底是怎么回事?

更新来更新:

HTML例子在这里: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> 

回答

2

documentation,你可以很容易地在number和地方(一个或多个)的address一个手柄,你做取:

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

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.

编辑 - 作为正确地指出resultsnearbySearch不要在result对象返回formatted_addressformatted_phone_number为此,你不得不做出另一个getdetails要求如你已经在你的代码中。它可能看起来像(未经测试):

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是提供给你使用它的职能范围,使外界宣告它的所有功能,并适当地初始化它像这样:

var service; //declare globally 
function intialize(){ 
    ... 
    service = new google.maps.places.PlacesService(map); //initialize here 

    ... 
} 

因此,您可以相应地格式化该内容字符串并填充infowindow。希望它能让你从正确的方向开始。

+0

一个问题。电话号码等未定义。有趣的是我正在看关于这个帖子的帖子。找到此:http://stackoverflow.com/questions/9520808/google-places-api-places-detail-request-undefined。试图实现代码。 –

+0

链接:http://stackoverflow.com/questions/9520808/google-places-api-places-detail-request-undefined –

+0

已更新。有一个新问题。 –

相关问题