如何使用Google商家信息获取地点名称数组? [英] How do I use Google Places to get an array of Place names?

查看:178
本文介绍了如何使用Google商家信息获取地点名称数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图访问Google商家信息,以便在经纬度范围内返回一组50个餐厅名称,但在提出请求时我碰到了一堵砖墙。我已经有了确定用户经纬度的代码,并且我已经从Google获得了我的API密钥,但我无法弄清楚如何通过URL发出请求,而Google文档没有提供如何实际使用的提示。 获取数据。我相信我能弄清楚如何解析JSON返回,但是获取数据比我想象的更加困难/混乱。

如果它很重要,我使用MaxMind来获取用户的经度和纬度。这是我迄今为止的脚本:

 < script type =text / javascriptsrc =https:/ /www.google.com/jsapi?key=api_key\"></script> 
< script type =text / javascriptsrc =http://j.maxmind.com/app/geoip.js>< / script>

< script type =text / javascript>
latitude = geoip_latitude();
longitude = geoip_longitude();
< / script>


解决方案


关于如何实际获取数据


并非如此, Places Web Service API文档包含示例 - 您可以使用适当的参数生成URL,然后获取解析的JSON或XML。



如果您使用的是jQuery和API支持的JSONP,您可以这样做:

  $。ajax({
url:'https://maps.googleapis.com/maps/api/place/search/json',
data:{
location: -33.8670522,151.1957362,
半径:500,
类型:'food',
名称:'harbor',
sensor:false,
key:'AIzaSyAiFpFd85eMtfbvmVNEYuNds5TEF9FjIPI '
},
成功:函数(数据){
//数据将是一个JSON对象,您可以遍历
},
dataType:'jsonp'
});

请注意,由于JSONP不受支持,所以这不起作用,这只是为了说明您可能在jQuery中发出请求



然而,AFAIK,web服务并不支持JSONP所以你将无法通过JavaScript发出请求。但是,您可以使用Google Maps JavaScript API并使用位置JavaScript库。再次,该页面有示例显示如何使用该库,基本用途将涉及启动服务并发出如下搜索请求(直接从文档复制):

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


I'm trying to access Google Places to return an array of 50 restaurant names around a latitude and longitude, but I've hit a brick wall when it comes to making the request. I've got the code to determine the user's latitude and longitude and I've gotten my API key from Google, I just cannot figure out how to make the request via the URL and the Google documentation is silent on how to actually get the data. I feel confident that I'll be able to figure out how to parse the JSON return, but getting the data has proven more difficult/confusing than I imagined.

If it matters, I'm using MaxMind to get the user's latitude and longitude. Here is the script that I have so far:

<script type="text/javascript" src="https://www.google.com/jsapi?key=api_key"></script>
<script type="text/javascript" src="http://j.maxmind.com/app/geoip.js"></script>

<script type="text/javascript">
    latitude = geoip_latitude();
    longitude = geoip_longitude();
</script>

解决方案

Google documentation is silent on how to actually get the data

Not really, the Places Web Service API documentation has examples - you generate the URL with the appropriate parameters and you get back JSON or XML which you parse.

If you were using jQuery and the API supported JSONP, you could have done something like this:

$.ajax({
        url: 'https://maps.googleapis.com/maps/api/place/search/json', 
        data: {
                location:"-33.8670522,151.1957362", 
                radius: 500, 
                types:'food', 
                name: 'harbour', 
                sensor: false, 
                key: 'AIzaSyAiFpFd85eMtfbvmVNEYuNds5TEF9FjIPI'
            }, 
        success: function(data){
                    //data will be a JSON object that you can iterate over
                },
        dataType: 'jsonp'
    });

Note that this will NOT work since JSONP is not supported, this is only to illustrate how you might make requests in jQuery

However, AFAIK, the web service does not support JSONP so you won't be able to make requests via JavaScript. You could, however, make use of the Google Maps JavaScript API and use the Places JavaScript library instead. Again, the page has examples showing how to use the library, the basic use will involve initiating the service and making a search request as below (copied straight from the docs):

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

这篇关于如何使用Google商家信息获取地点名称数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆