检索当前位置的附近位置

时间:2012-07-18 07:17:43

标签: android google-places-api

我正在开发一个应用程序,我需要检索当前位置附近的餐馆。论坛建议我使用Google Places API。 当我读到Google Place API时,它说项目需要是“Google App Engine项目”+ Android客户端。但我感到困惑的是,这是一种正确的做法,因为我对Google世界还不熟悉。

有人可以建议我哪个是检索当前位置附近餐馆的最佳方法吗?

感谢。

1 个答案:

答案 0 :(得分:0)

点击此链接。 为您的应用创建应用密钥&你只需要发出一个Http请求,&你将获得Xml // json中的所有信息。 https://developers.google.com/places/documentation/

https://maps.googleapis.com/maps/api/place/search/xml?location=-33.8670522,151.1957362&radius=500&types=food&name=harbour&sensor=false&key=AddYourOwnKeyHere

在上面的网址中,添加你的Api密钥& locaton。它会向你返回一个xml,其中包含500米圆圈内的所有restras。

此网址只接受获取请求。 Yov必须在Url本身附加所有参数。

@Override
                public void run() 
                {
                     String params="location="+(location.getLatitude())+","+(location.getLongitude())+"&radius=5000&types=food&sensor=false&key=your kay";
                        try
                        {
                            http.getPlaces(new URL("https://maps.googleapis.com/maps/api/place/search/xml?"+params));
                            Log.i("url", "https://maps.googleapis.com/maps/api/place/search/xml?"+params);
                            setList();
                        } 
                        catch (MalformedURLException e)
                        {
                            e.printStackTrace();
                        }
                        catch (Exception e) 
                        {
                            e.printStackTrace();
                        }

                }
            });


public void getPlacesDetails(URL url) throws Exception {
    try 
    {
        connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setUseCaches(false);

        connection.setConnectTimeout(TIMEOUT_CONNECT_MILLIS);
        connection.setReadTimeout(TIMEOUT_READ_MILLIS);
        connection.setRequestMethod("GET");

        inStream = (InputStream) connection.getInputStream();


        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();

        HospitalDetailParser myXMLHandler = new HospitalDetailParser();
        xr.setContentHandler(myXMLHandler);
        xr.parse(new InputSource(inStream));


    }

    catch (MalformedURLException e) 
    {

        Log.i("exception in sms", "" + e.toString());
        throw e;
    }

    catch (Exception e) 
    {

        Log.i("exception in sms", "" + e.toString());
        throw e;
    }

}