2

我正在编写一个应用程序,用于使用 Google Maps & Places API 查找附近的餐厅和其他景点。我正在使用谷歌地方搜索功能来帮助获取信息

var service = new google.maps.places.PlacesService(map);
service.search(request, callback);

但后来我意识到它不会返回我需要的值,比如我使用搜索创建的标记的格式化地址。现在我感到困惑的是要获得使用该getDetails()功能所需的额外信息。这应该替换我上面使用的搜索功能还是应该在此之后放置一段时间?当谷歌在他们的网站上描述它时,它看起来应该只是替换搜索功能,因为在示例中它采用相同的确切参数并且运行与搜索功能相同但是如果我这样做,那么它根本不会返回任何地方。这是我的一些代码,可帮助解释我要完成的工作。

  //Function for finding destination types. Receives destination type as a string.
function findDestinationType(where)
{
    request = null;
    var request = 
    {
        location: point,
        radius: 2500,
        types: [where]
    };

    var service = new google.maps.places.PlacesService(map);
    service.getDetails(request, callback);
}
//Call Back to fill array with markers & locations 
function callback(results, status) 
{
    if (status == google.maps.places.PlacesServiceStatus.OK) 
    {
        initialize();
        iterator = 0;
        markerArr = [];
        for (var i = 0; i < results.length; i++) 
        {
            markerArr.push(results[i].geometry.location);
            result = results[i];
            createMarker(results[i]);
        }
    }
    else
    {
        alert("Sorry, there are no locations in your area");
    }
}
//Function to create marker
function createMarker(place)
{
    var marker = new google.maps.Marker(
    {
        position: markerArr[iterator],
        map: map,
        draggable: false,
        animation: google.maps.Animation.DROP
    })
    //alert(markerArr[iterator]);
    markersA.push(marker);
    iterator++;
    google.maps.event.addListener(marker, 'click', function()
    {
        console.log('clicked');
        infowindow.setContent(place.name);
        infowindow.open(map, this);
        //directionsDisplay.setMap(map);
    });
}
4

1 回答 1

4

Your assumption is wrong, getDetails() expects not the same parameters, it expects a reference to a place.

The reference you will get as a result for a places-search, it's a token.

So the workflow is when you search for a place:

  1. use places.search() for basic informations about the place
  2. when you need more informations use places.getDetails() with the reference from step 1. as argument
于 2012-08-07T18:22:52.533 回答