我想知道"How to find Hospital Location near by my location "使用android,怎么可能?,我可以使用谷歌的数据库获取医院位置的纬度和经度值,怎么可能?
谢谢大家
一步步,
Google place api用于访问附近的anloaction地标
第1步:转到API控制台以获取Place API
https://code.google.com/apis/console/
并在服务选项卡上选择

在地方服务

现在选择API Access选项卡并获取API KEY

现在你有一个用于获取位置的API密钥
现在在编程
*步骤2* :首先创建一个名为Place.java的类.此类用于包含Place api提供的场所属性.
package com.android.code.GoogleMap.NearsetLandmark;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONException;
import org.json.JSONObject;
public class Place {
private String id;
private String icon;
private String name;
private String vicinity;
private Double latitude;
private Double longitude;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
public Double getLatitude() {
return latitude;
}
public void setLatitude(Double latitude) {
this.latitude = latitude;
}
public Double getLongitude() {
return longitude;
}
public void setLongitude(Double longitude) {
this.longitude = longitude;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getVicinity() {
return vicinity;
}
public void setVicinity(String vicinity) {
this.vicinity = vicinity;
}
static Place jsonToPontoReferencia(JSONObject pontoReferencia) {
try {
Place result = new Place();
JSONObject geometry = (JSONObject) pontoReferencia.get("geometry");
JSONObject location = (JSONObject) geometry.get("location");
result.setLatitude((Double) location.get("lat"));
result.setLongitude((Double) location.get("lng"));
result.setIcon(pontoReferencia.getString("icon"));
result.setName(pontoReferencia.getString("name"));
result.setVicinity(pontoReferencia.getString("vicinity"));
result.setId(pontoReferencia.getString("id"));
return result;
} catch (JSONException ex) {
Logger.getLogger(Place.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
@Override
public String toString() {
return "Place{" + "id=" + id + ", icon=" + icon + ", name=" + name + ", latitude=" + latitude + ", longitude=" + longitude + '}';
}
}
Run Code Online (Sandbox Code Playgroud)
现在创建一个名为PlacesService的类
package com.android.code.GoogleMap.NearsetLandmark;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
public class PlacesService {
private String API_KEY;
public PlacesService(String apikey) {
this.API_KEY = apikey;
}
public void setApiKey(String apikey) {
this.API_KEY = apikey;
}
public List<Place> findPlaces(double latitude, double longitude,String placeSpacification)
{
String urlString = makeUrl(latitude, longitude,placeSpacification);
try {
String json = getJSON(urlString);
System.out.println(json);
JSONObject object = new JSONObject(json);
JSONArray array = object.getJSONArray("results");
ArrayList<Place> arrayList = new ArrayList<Place>();
for (int i = 0; i < array.length(); i++) {
try {
Place place = Place.jsonToPontoReferencia((JSONObject) array.get(i));
Log.v("Places Services ", ""+place);
arrayList.add(place);
} catch (Exception e) {
}
}
return arrayList;
} catch (JSONException ex) {
Logger.getLogger(PlacesService.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
//https://maps.googleapis.com/maps/api/place/search/json?location=28.632808,77.218276&radius=500&types=atm&sensor=false&key=apikey
private String makeUrl(double latitude, double longitude,String place) {
StringBuilder urlString = new StringBuilder("https://maps.googleapis.com/maps/api/place/search/json?");
if (place.equals("")) {
urlString.append("&location=");
urlString.append(Double.toString(latitude));
urlString.append(",");
urlString.append(Double.toString(longitude));
urlString.append("&radius=1000");
// urlString.append("&types="+place);
urlString.append("&sensor=false&key=" + API_KEY);
} else {
urlString.append("&location=");
urlString.append(Double.toString(latitude));
urlString.append(",");
urlString.append(Double.toString(longitude));
urlString.append("&radius=1000");
urlString.append("&types="+place);
urlString.append("&sensor=false&key=" + API_KEY);
}
return urlString.toString();
}
protected String getJSON(String url) {
return getUrlContents(url);
}
private String getUrlContents(String theUrl)
{
StringBuilder content = new StringBuilder();
try {
URL url = new URL(theUrl);
URLConnection urlConnection = url.openConnection();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()), 8);
String line;
while ((line = bufferedReader.readLine()) != null)
{
content.append(line + "\n");
}
bufferedReader.close();
}
catch (Exception e)
{
e.printStackTrace();
}
return content.toString();
}
}
Run Code Online (Sandbox Code Playgroud)
现在创建一个新的Activity,在其中获取最近的位置列表.
/****/
package com.android.code.GoogleMap.NearsetLandmark;
public class CheckInActivity extends ListActivity{
@Override
protected void onCreate(Bundle arg0) {
// TODO Auto-generated method stub
super.onCreate(arg0);
new GetPlaces(this, getListView()).execute();
}
class GetPlaces extends AsyncTask<Void, Void, Void>{
private ProgressDialog dialog;
private Context context;
private String[] placeName;
private String[] imageUrl;
private ListView listView;
public GetPlaces(Context context, ListView listView) {
// TODO Auto-generated constructor stub
this.context = context;
this.listView = listView;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
dialog.dismiss();
listView.setAdapter(new ArrayAdapter<String>(context, android.R.layout.simple_expandable_list_item_1,placeName));
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
dialog = new ProgressDialog(context);
dialog.setCancelable(true);
dialog.setMessage("Loading..");
dialog.isIndeterminate();
dialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
PlacesService service = new PlacesService("paste your place api key");
List<Place> findPlaces = service.findPlaces(28.632808,77.218276,"hospital"); // hospiral for hospital
// atm for ATM
placeName = new String[findPlaces.size()];
imageUrl = new String[findPlaces.size()];
for (int i = 0; i < findPlaces.size(); i++) {
Place placeDetail = findPlaces.get(i);
placeDetail.getIcon();
System.out.println( placeDetail.getName());
placeName[i] =placeDetail.getName();
imageUrl[i] =placeDetail.getIcon();
}
return null;
}
}
}
Run Code Online (Sandbox Code Playgroud)
你可以解析xml:http://maps.google.com/maps? q = hospital&mrt = osc&else = lalat,en&output = kml,其中lat和lon是你的纬度和经度坐标.
小智 3
距离计算基于 LatLong 数学,如下所示:
\n\n距离
\n\n这里使用\xe2\x80\x98haversine\xe2\x80\x99公式来计算两点之间的大圆距离\xe2\x80\x93,即地球表面\xe2\x80\x99s表面\xe2的最短距离\x80\x93 给出点之间的 \xe2\x80\x98as-the-crow-flies\xe2\x80\x99 距离(忽略任何山丘!)。
\n\n半正矢公式:
\n\nR = earth\xe2\x80\x99s radius (mean radius = 6,371km)\n\xce\x94lat = lat2\xe2\x88\x92 lat1\n\xce\x94long = long2\xe2\x88\x92 long1\na = sin\xc2\xb2(\xce\x94lat/2) + cos(lat1).cos(lat2).sin\xc2\xb2(\xce\x94long/2)\nc = 2.atan2(\xe2\x88\x9aa, \xe2\x88\x9a(1\xe2\x88\x92a))\nd = R.c\nRun Code Online (Sandbox Code Playgroud)\n\n(请注意,角度需要以弧度为单位才能传递给三角函数)。\nJavaScript:
\n\nvar R = 6371; // km\nvar dLat = (lat2-lat1).toRad();\nvar dLon = (lon2-lon1).toRad(); \nvar a = Math.sin(dLat/2) * Math.sin(dLat/2) +\n Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) * \n Math.sin(dLon/2) * Math.sin(dLon/2); \nvar c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); \nvar d = R * c;\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
14774 次 |
| 最近记录: |