使用谷歌地点API

36

我想开发一个应用程序,可以通过获取用户的当前位置,在应用程序中显示附近(1公里内)的地点信息。

例如,我想显示与Android设备当前位置相关的餐馆、购物中心、医院等地点信息。

我已经阅读了这个链接:在Android中使用Google Places API,但是没有得到更多的帮助。

有人在Android中使用过Google Places API吗?


1
你是否查看了Google Places API in Android问题的答案中的教程链接? - Harry Joy
@Harry Joy:是的。我在那段代码中无法得到太多信息。那段代码到底在做什么?..那里需要inputUrl的原因是什么? - Kartik Domadiya
keyStringGoogle 地图 API 密钥inputUrl 用于测试目的。您可以省略它并使用在 urlString 中声明的 URL。keyStringurlString 应由您定义。 - Harry Joy
这是你想要使用Google地图密钥签名的URL。教程代码有良好的注释,易于理解。 - Harry Joy
我得到了这样的响应: { "error_message" : "此IP、站点或移动应用程序未被授权使用此API密钥。请求来自IP地址183.83.193.68,引荐为空", "html_attributions" : [], "results" : [], "status" : "REQUEST_DENIED" } - Harsha
显示剩余7条评论
5个回答

25

需要注意以下几点:

  • 要使用Google Places API,您需要注册并获取Google地图API密钥。
  • 在教程中的UrlSigner类中,变量描述如下:keyString => 是您的Google地图API密钥。 urlString是您想使用API密钥签名的URL。
  • 在代码中,您会发现inputKeyinputUrl这两个变量仅用于测试目的,如果您希望可以忽略它们。您可以直接编写以下代码:

    URL url = new URL(urlString);

    UrlSigner signer = new UrlSigner(keyString);

    String request = signer.signRequest(url.getPath(),url.getQuery());

    System.out.println("Signed URL :" + url.getProtocol() + "://" + url.getHost() + request);

放在UrlSigner类的主方法中。


我没有谷歌广告账户。是否必须拥有谷歌广告账户?当我阅读一些谷歌的文档时,他们推荐这样做。 - Kartik Domadiya
@Kartik:不需要。你只需要在 Gmail 中登录即可。 - Harry Joy
我现在遇到了同样的问题...我需要填写什么来代替clientIdSIGNATURE?我只有一个谷歌地图密钥... http://maps.google.com/maps/api/place/search/json?location=40.717859,-73.9577937&radius=1600&client=clientId&sensor=true_or_false&signature=SIGNATURE - lis
8
链接已过时。 - ljeabmreosn
2
链接指向广告...这是垃圾邮件...请删除它。 - Samir Alakbarov

12

这个API在印度(当然在全球的任何地方都可以)运作良好。你需要做的是使用谷歌地点API。你可以向谷歌发送一个URL请求,其中包括用户当前位置和您想要的场所类型。

响应可以是XML或Json格式,根据您的请求而定。您只需要解析它即可。您可以获取您周围50公里内的地点。一个URL请求的示例如下:

https://maps.googleapis.com/maps/api/place/search/xml?location=9.934866,76.267235&radius=50000&types=country%7Cairport%7Camusement_park%7Cbank%7Cbook_store%7Cbus_station%7Ccafe%7Ccar_rental%7Ccar_repair%7Cchurch%7Cdoctor%7Cfire_station%7Cfood%7Chindu_temple%7Chospital%7Clawyer%7Clibrary%7Cmosque%7Cmuseum%7Cpark%7Cparking%7Cpharmacy%7Cpolice%7Cpost_office%7Crestaurant%7Cschool%7Ctrain_station%7Czoo&sensor=true&key=your_API_Key

*Note: %7C is just "|".


*注意: %7C 只是代表 "|" 。

4

@ddewaele... 我看了你博客上的链接并下载了代码。但是我不明白为什么没有res、android manifest文件和文件夹?还有pom.xml是什么?你为什么要创建它?请解释一下。 - Rashmi.B
1
如答案所述,示例仅涵盖Java的Google APIs客户端库和Google Places API。它可以在Android应用程序中嵌入而不需要太多更改。 - ddewaele
感谢您的谦虚回复ddewaele。我是一个Android的初学者,正在开发一个银行定位器应用。在检测到我的当前位置后,您能帮我了解下一步该怎么做吗?开发这个定位器应用程序需要哪些先决条件? - Rashmi.B
我建议您撰写一个新问题,清楚地描述您想要做什么,您已经尝试了什么以及出了什么问题。 - ddewaele

1
请注意,该教程已经写了近一年的时间。自那时起,Places API已经向公众推出,因此注册过程现在变得更加简单-请参阅以下文档: http://code.google.com/apis/maps/documentation/places/#Limits 现在还有一些新功能可用,包括按类别搜索,提供100多个不同的类别,并提供自动完成服务: http://code.google.com/apis/maps/documentation/places/autocomplete.html 特别是,分类搜索应该可以帮助您找到餐厅、购物中心和医院等特定场所。

0
/*
For google api key:-
login to your mail account
go to https://code.google.com/apis/console/
goto services tab
click on places api and google map api button. Fill the details
goto api access tab and copy your google api key
*/



package com.example.jstest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.Bundle;
import android.app.Activity;
import android.widget.Toast;

public class MainActivity extends Activity {
    private String googleAPIKey = "your google api key"; 
    DefaultHttpClient client;
    HttpResponse res;
    HttpGet req;
    InputStream in;
    JSONObject jsonobj;
    JSONArray resarray;
    String requesturl;
    HttpEntity jsonentity;

    final String TAG = getClass().getSimpleName();  


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        requesturl="https://maps.googleapis.com/maps/api/place/search/json?radius=500&sensor=false&key="+googleAPIKey+"&location=13.01,74.79";

        System.out.println("Request "+requesturl);
        client=new DefaultHttpClient();
        System.out.println("hello");

        req=new HttpGet(requesturl);
        System.out.println("hello");

        try {
            res=client.execute(req);
            StatusLine status = res.getStatusLine();
            int code = status.getStatusCode();
            System.out.println(code);
            if(code!=200)
            {
                System.out.println("Request Has not succeeded");
                finish();
            }

            jsonentity=res.getEntity();
            in = jsonentity.getContent();

            jsonobj=new JSONObject(convertStreamToString(in));


            resarray = jsonobj.getJSONArray("results");

            if(resarray.length()==0){
            }
            else{
                int len=resarray.length();
                for(int j=0;j<len;j++)
                {
                    Toast.makeText(getApplicationContext(), resarray.getJSONObject(j).getString("name") , Toast.LENGTH_LONG).show();
                }
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }   
        catch (JSONException e) {
            e.printStackTrace();
        }   
    }

    private String convertStreamToString(InputStream in) {
        BufferedReader br=new BufferedReader(new InputStreamReader(in));
        StringBuilder jsonstr=new StringBuilder();
        String line;
        try {
            while((line=br.readLine())!=null)
            {
                String t=line+"\n";
                jsonstr.append(t);
            }
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return jsonstr.toString();
    }  

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接