我想开发一个应用程序,我想通过获取用户的当前位置来显示用户附近(1公里内)的信息。
例如,我想显示与 android 设备当前位置相关的 1 公里范围内的餐厅、购物中心、医院的信息。
我已经浏览了这个链接:Using Google Places API in Android ..但没有得到更多。
有人在 android 中使用过 Google Places API 吗?
我想开发一个应用程序,我想通过获取用户的当前位置来显示用户附近(1公里内)的信息。
例如,我想显示与 android 设备当前位置相关的 1 公里范围内的餐厅、购物中心、医院的信息。
我已经浏览了这个链接:Using Google Places API in Android ..但没有得到更多。
有人在 android 中使用过 Google Places API 吗?
需要记住的几点:
keyString
=> 是您的 Google Map api 密钥。urlString
是您要使用 api 密钥签名的 url。在代码中你会发现inputKey
和inputUrl
。这 2 个变量仅用于测试目的,您可以根据需要省略它们。可以直接写代码如下:
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 类的主要方法中。
该 API 在印度运行良好(当然可以在全球任何地方运行)。您必须通过的是Google Places API。您可以简单地使用用户的当前位置和您喜欢的地点类型向 google 发出 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
*注意:%7C 只是“|”。
Google Places API 几周前公开了。
使用适用于 Java 的 Android 兼容Google API 客户端库,您可以从 Android 运行时使用 Google Places API。
Google Places API REST API有据可查。有关使用适用于 Java 的 Google API 客户端库和 Google Places API 的示例,请查看以下博客条目:
http://ddewaele.blogspot.com/2011/05/introducing-google-places-api.html
请记住,该教程是大约一年前编写的。从那时起,Places API 已向公众发布,因此注册过程现在更加简单 - 请参阅以下文档: http ://code.google.com/apis/maps/documentation/places/#Limits
现在还提供新功能,包括按类别搜索,提供 100 多个不同的类别,以及自动完成服务: http ://code.google.com/apis/maps/documentation/places/autocomplete.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();
}
}