我想开发一个应用程序,可以通过获取用户的当前位置,在应用程序中显示附近(1公里内)的地点信息。
例如,我想显示与Android设备当前位置相关的餐馆、购物中心、医院等地点信息。
我已经阅读了这个链接:在Android中使用Google Places API,但是没有得到更多的帮助。
有人在Android中使用过Google Places API吗?
我想开发一个应用程序,可以通过获取用户的当前位置,在应用程序中显示附近(1公里内)的地点信息。
例如,我想显示与Android设备当前位置相关的餐馆、购物中心、医院等地点信息。
我已经阅读了这个链接:在Android中使用Google Places API,但是没有得到更多的帮助。
有人在Android中使用过Google Places API吗?
需要注意以下几点:
keyString
=> 是您的Google地图API密钥。 urlString
是您想使用API密钥签名的URL。在代码中,您会发现inputKey
和inputUrl
这两个变量仅用于测试目的,如果您希望可以忽略它们。您可以直接编写以下代码:
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类的主方法中。
clientId
和SIGNATURE
?我只有一个谷歌地图密钥... 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这个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 "|".
http://ddewaele.blogspot.com/2011/05/introducing-google-places-api.html
/*
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();
}
}
keyString
是 Google 地图 API 密钥,inputUrl
用于测试目的。您可以省略它并使用在urlString
中声明的 URL。keyString
和urlString
应由您定义。 - Harry Joy