如何获取Google Maps API v3的搜索字符串参数

时间:2013-07-28 20:00:11

标签: google-maps google-maps-api-3

我正在尝试根据用户的查询获取所有附近的兴趣点,例如“附近的自动取款机”或“附近的医院”或“附近的巴士站”。我使用了以下js代码:

function initialize_search() {

var input = document.getElementById('search');
var autocomplete = new google.maps.places.Autocomplete(input);
}

google.maps.event.addDomListener(window, 'load', initialize);

var resultList = [];
var service = new google.maps.places.PlacesService(map);
var search = document.getElementById('search').value;  
var request = {
    location: map.getCenter(),
    radius: '5000',
    query: search,            
};

service.textSearch(request, function(results, status, pagination){
    if (status == google.maps.places.PlacesServiceStatus.OK) {
        resultList = resultList.concat(results);
        plotResultList();
    }
});

var overlays = [];

function plotResultList(){
    var iterator = 0;                  
    for(var i = 0; i < resultList.length; i++){
        setTimeout(function(){

            var marker = new google.maps.Marker({
                position: resultList[iterator].geometry.position,
                map: map,
                title: resultList[iterator].name,
                animation: google.maps.Animation.DROP
            });

            overlays.push(marker);

            iterator++;
        }, i * 75);
    }
}

var infoWindow = new google.maps.InfoWindow();      
google.maps.event.addListener(marker, 'click', function() {
    infoWindow.close();
    var request = {
        reference: resultList[iterator].reference
    };

    service.getDetails(request, function(place, status){
        var content = "<h2>" + name + "</h2>";
        if(status == google.maps.places.PlacesServiceStatus.OK){    
            if(typeof place.rating !== 'undefined'){
                content += "<p><small>Rating: " + place.rating + "</small></p>"; 
            }    

            if(typeof place.formatted_address !== 'undefined'){
                content += "<br><small>" + place.formatted_address + "</small>";
            }

            if(typeof place.formatted_phone_number !== 'undefined'){
                content += "<br><small><a href='tel:" + place.formatted_phone_number + "'>" + place.formatted_phone_number + "</a></small>";                                 
            }

            if(typeof place.website !== 'undefined'){
                content += "<br><small><a href='" + place.website + "'>website</a></small>";

            }
        }                            

        infoWindow.setContent(content);
        infoWindow.open(map, marker);         
    });
});

我使用的HTML代码是:

<div id="search">
      <input id="target" type="text" placeholder="Search Box">
      <button onclick="plotResultList();">Search</button>
    </div>
    <div id="map-canvas"></div>

现在当我在浏览器中加载时,搜索框无效。 Niether是否为地方提供建议。当我在谷歌地图网站上输入“附近的自动取款机”时,它们会显示附近的所有自动柜员机。我如何在我的代码中实现这一点?

0 个答案:

没有答案