Google Maps API V3 和本地搜索问题 - 空结果?

Posted

技术标签:

【中文标题】Google Maps API V3 和本地搜索问题 - 空结果?【英文标题】:Google Maps API V3 and Local search problem - empty results? 【发布时间】:2010-08-11 15:10:38 【问题描述】:

我正在尝试实现 Maps API V3 和本地搜索,但我似乎遇到了问题。不知何故,OnLocalSearch() 函数中的结果为空。

这是我的完整代码:

<script type="text/javascript">
//<![CDATA[
$(document).ready(function() 
    // do stuff when DOM is ready
    var geocoder = new google.maps.Geocoder();
    var address = 'string_location';
    var map;

    // Our global state for LocalSearch
    var gInfoWindow;
    var gSelectedResults = [];
    var gCurrentResults = [];
    var gLocalSearch = new GlocalSearch();

    if (geocoder) 
        geocoder.geocode( 'address': address , function (results, status) 
            if (status == google.maps.GeocoderStatus.OK) 
                //alert(results[0].geometry.location.lat())
                //alert(results[0].geometry.location.lng())

                //Create the Map and center to geocode results latlong
                var latlng = new google.maps.LatLng(results[0].geometry.location.lat(), results[0].geometry.location.lng());
                var myOptions = 
                    zoom: 14,
                    center: latlng,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                ;

                map = new google.maps.Map(document.getElementById("map_canvas"),
                    myOptions);

                gLocalSearch.setSearchCompleteCallback(this, OnLocalSearch);
                gLocalSearch.execute("business_item.name");
             
            else 
                alert('No results found. Check console.log()');
                console.log("Geocoding address: " + address);
                console.log("Geocoding failed: " + status);
            
        );
    

    /*
    Other functions        
    */        
    function OnLocalSearch() 
        if (gLocalSearch.results[0])  //This is empty. Why?
            var resultLat = gLocalSearch.results[0].lat; 
            var resultLng = gLocalSearch.results[0].lng; 
            var point = new GLatLng(resultLat,resultLng); 
            callbackFunction(point); 
        else 
            alert("not found!"); 
         
    
);
//]]>
</script>

仅供参考,我以这个为例,我现在被困了几个小时:http://gmaps-samples-v3.googlecode.com/svn-history/r136/trunk/localsearch/places.html

任何回复将不胜感激。

问候, 温伯特

更新 我在这里的某个地方犯了一个错误:

<script src="http://www.google.com/uds/api?file=uds.js&v=1.0" type="text/javascript"><;/script>
<script src="http://maps.google.com/maps/api/js?v=3.1&sensor=false&region=PH"></script>

另外,请务必仔细检查您正在地理编码的地址。我来自菲律宾,谷歌似乎只对主要道路进行地理编码。见http://gmaps-samples.googlecode.com/svn/trunk/mapcoverage_filtered.html

感谢 irc.geekshed.net 的 jgeerdes #googleapis

【问题讨论】:

我们如何通过字符串获取原始(准确)名称..? 【参考方案1】:

只需进行一些调整以使代码真正完整,并使用我知道将成功进行地理编码的地址加上我知道将返回某些内容的查询,您的代码就可以工作。这是我所做的:

<html>
<head>
 <title>Wenbert test</title>
<script src="http://www.google.com/jsapi"></script>
 <script type="text/javascript">
//<![CDATA[
google.load('jquery','1.4.2');
google.load('maps','3',other_params:'sensor=false');
google.load('search','1');
alert('starting...');

$(document).ready(function() 
alert('here');
    // do stuff when DOM is ready
    var geocoder = new google.maps.Geocoder();
    var address = '4019 lower beaver rd. 50310';
    var map;

    // Our global state for LocalSearch
    var gInfoWindow;
    var gSelectedResults = [];
    var gCurrentResults = [];
    var gLocalSearch = new GlocalSearch();

    if (geocoder) 
        geocoder.geocode( 'address': address , function (results, status) 
            if (status == google.maps.GeocoderStatus.OK) 
                //alert(results[0].geometry.location.lat())
                //alert(results[0].geometry.location.lng())

                //Create the Map and center to geocode results latlong
                var latlng = new google.maps.LatLng(results[0].geometry.location.lat(), results[0].geometry.location.lng());
                var myOptions = 
                    zoom: 14,
                    center: latlng,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                ;

                map = new google.maps.Map(document.getElementById("map_canvas"),
                    myOptions);

                gLocalSearch.setSearchCompleteCallback(this, OnLocalSearch);
                gLocalSearch.execute("debra heights wesleyan church");
             
            else 
                alert('No results found. Check console.log()');
                console.log("Geocoding address: " + address);
                console.log("Geocoding failed: " + status);
            
        );
    

    /*
    Other functions        
    */        
    function OnLocalSearch() 
        if (gLocalSearch.results[0])  //This is empty. Why?
            var resultLat = gLocalSearch.results[0].lat; 
            var resultLng = gLocalSearch.results[0].lng; 
            var point = new google.maps.LatLng(resultLat,resultLng); 
            callbackFunction(point); 
        else 
            alert("not found!"); 
         
    
);
//]]>
</script>
</head>
<body>
 <div id="map_canvas" style="height:100%;"></div>
</body>
</html>

【讨论】:

谢谢!看起来我犯了错误,包括标题中的 javascript。

以上是关于Google Maps API V3 和本地搜索问题 - 空结果?的主要内容,如果未能解决你的问题,请参考以下文章

如何自定义附近地点搜索返回的结果,Google Maps JS API V3

Google Maps API V3 - 边界内最近的路线

Google maps API V3 - 无法对 AutocompleteService 预测进行地理编码

将 Google Maps API v3 搜索结果限制在某个国家/地区,同时将它们偏向某个位置

带标签的 Google Maps API v3 标记

如何从 Google Maps v3 API 中删除带有 mysql 数据的标记?