首页 文章

使用Google Places API

提问于
浏览
30

我想开发一个应用程序,我希望通过获取当前位置来显示用户附近(1公里内)附近地点的信息 .

比如说我想显示有关距离Android设备当前位置1公里范围内的餐馆,购物中心,医院的信息 .

我已经通过这个链接:Using Google Places API in Android ..但没有得到更多 .

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

5 回答

  • 1

    试试这个教程:using-googles-places-api,无论遇到什么问题,请在评论中提出 .

    要记住一些要点:

    • 要使用Google的Places API,您需要注册并获取Google Map的API密钥 .

    • 在教程中的类UrlSigner中,变量描述为: keyString =>是您的Google Map api密钥 . urlString 是您要使用api密钥签名的网址 .

    • 在代码中,您将找到 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类的主要方法中 .

  • 12

    API在印度运作良好(当然可以在全球任何地方使用) . 你必须经历的是Google Places API . 您可以简单地向用户的当前位置和您喜欢的地点类型提供网址请求 .

    根据您的请求,响应可以是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
    

    *注意:%7C只是“|” .

  • 26

    Google Places API几周前上市 .

    使用Android兼容Google APIs Client Library for Java,您可以使用Android运行时的Google Places API .

    Google Places API REST API有详细记录 . 有关使用Google API Client Library for Java和Google Places API的示例,请查看以下博客条目:

    http://ddewaele.blogspot.com/2011/05/introducing-google-places-api.html

  • 4

    请记住,该教程是在大约一年前编写的 . 从那时起,Places API已经向公众推出,因此注册过程现在变得更加简单 - 请参阅以下文档:http://code.google.com/apis/maps/documentation/places/#Limits

    现在还有新功能,包括按类别搜索,提供超过100种不同类别,以及自动填充服务:http://code.google.com/apis/maps/documentation/places/autocomplete.html

    特别是,类别搜索应该帮助您专门找到餐馆,购物中心和医院 .

  • 1
    /*
    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();
        }  
    
    }
    

相关问题