【问题标题】:Google Maps JS API equivalent of URL相当于 URL 的 Google Maps JS API
【发布时间】:2015-02-17 16:21:58
【问题描述】:

我刚刚意识到,由于 CORS,我无法从我的 AngularJS 应用程序中直接调用 URL。因此,我希望我将不得不使用 Javascript API。

以下链接为我提供了我想要的数据:

https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=' + myPosition.lat + ',' + myPosition.lng + '&rankby=distance&key=' + key  + '&sensor=false&type=clothing_store

但是,我不知道如何使用 Javascript API 来实现相同的目的。我在文档中找到了this,但我不需要地图——只需要附近商店的名称和坐标。

如何使用 Javascript API 获取相同的数据?

【问题讨论】:

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


    【解决方案1】:

    如果您已经拥有 Google Maps for JS API Map Key,您可以从服务器端执行 a Places query。但是请注意,您仅限于1000 queries per day

    您可能可以直接在客户端中执行此操作并避免 CORS 问题,但这是我之前使用 PHP 完成的方法:

    <?php
        function ReturnEmpty()
        {
            // Something's wrong, return an empty set.
            echo '{"d":[]}';
        }
    
        $q = (isset($_GET["query"])) ? $_GET["query"] : 'NULL';
    
    
        if( $q == "" )
        {
            ReturnEmpty();
        }
        else
        {
            // I'm showing the "textsearch" option, but there is also a "nearbysearch" option..
            $url = "https://maps.googleapis.com/maps/api/place/textsearch/json?query="
                    . urlencode($q)
                    . "&key=YOUR_GOOGLE_MAPS_KEY_HERE";
    
            $json = file_get_contents($url);
    
            $data = json_decode($json, true);
    
            echo $data;
        }
    ?>
    

    【讨论】:

      【解决方案2】:

      您可以在google.maps.places.PlacesService(map) 上使用nearbySearch(request, callback) 方法。

      在您的情况下,request 应该有一个 location(包含 lat 和 lng)和一个 radius

      有代码示例here

      【讨论】:

      • 那仍然需要一个 map 对象,我没有这个对象
      猜你喜欢
      • 2011-11-30
      • 1970-01-01
      • 1970-01-01
      • 2015-11-12
      • 2010-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多