How To Find Hospital Location Near By My Location?
Solution 1:
you can parse the xml : http://maps.google.com/maps?q=hospital&mrt=yp&sll=lat,lon&output=kml where lat and lon are your latitude and longitude coordinates.
Solution 2:
Step by step,
Google place api are used to access near by landmark of anloaction
Step 1 : Go to API Console for obtaining the Place API
https://code.google.com/apis/console/
and select on services tab
on the place service
now select API Access tab and get the API KEY
now you have a API key for getting place
Now in programming
*Step 2 * : first create a class named Place.java. This class is used to contain the property of place which are provided by 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;
publicclassPlace {
privateString id;
privateString icon;
privateString name;
privateString vicinity;
privateDouble latitude;
privateDouble longitude;
publicStringgetId() {
return id;
}
publicvoidsetId(String id) {
this.id = id;
}
publicStringgetIcon() {
return icon;
}
publicvoidsetIcon(String icon) {
this.icon = icon;
}
publicDoublegetLatitude() {
return latitude;
}
publicvoidsetLatitude(Double latitude) {
this.latitude = latitude;
}
publicDoublegetLongitude() {
return longitude;
}
publicvoidsetLongitude(Double longitude) {
this.longitude = longitude;
}
publicStringgetName() {
return name;
}
publicvoidsetName(String name) {
this.name = name;
}
publicStringgetVicinity() {
return vicinity;
}
publicvoidsetVicinity(String vicinity) {
this.vicinity = vicinity;
}
staticPlacejsonToPontoReferencia(JSONObject pontoReferencia) {
try {
Place result = newPlace();
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);
}
returnnull;
}
@OverridepublicStringtoString() {
return"Place{" + "id=" + id + ", icon=" + icon + ", name=" + name + ", latitude=" + latitude + ", longitude=" + longitude + '}';
}
}
Now create a class named 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;
publicclassPlacesService {
private String API_KEY;
publicPlacesService(String apikey) {
this.API_KEY = apikey;
}
publicvoidsetApiKey(String apikey) {
this.API_KEY = apikey;
}
public List<Place> findPlaces(double latitude, double longitude,String placeSpacification)
{
StringurlString= makeUrl(latitude, longitude,placeSpacification);
try {
Stringjson= getJSON(urlString);
System.out.println(json);
JSONObjectobject=newJSONObject(json);
JSONArrayarray= object.getJSONArray("results");
ArrayList<Place> arrayList = newArrayList<Place>();
for (inti=0; i < array.length(); i++) {
try {
Placeplace= 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);
}
returnnull;
}
//https://maps.googleapis.com/maps/api/place/search/json?location=28.632808,77.218276&radius=500&types=atm&sensor=false&key=apikeyprivate String makeUrl(double latitude, double longitude,String place) {
StringBuilderurlString=newStringBuilder("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)
{
StringBuildercontent=newStringBuilder();
try {
URLurl=newURL(theUrl);
URLConnectionurlConnection= url.openConnection();
BufferedReaderbufferedReader=newBufferedReader(newInputStreamReader(urlConnection.getInputStream()), 8);
String line;
while ((line = bufferedReader.readLine()) != null)
{
content.append(line + "\n");
}
bufferedReader.close();
}
catch (Exception e)
{
e.printStackTrace();
}
return content.toString();
}
}
Now create a new Activity where you want to get the list of nearest places.
/** * */
package com.android.code.GoogleMap.NearsetLandmark;
publicclassCheckInActivityextendsListActivity{
@OverrideprotectedvoidonCreate(Bundle arg0) {
// TODO Auto-generated method stubsuper.onCreate(arg0);
newGetPlaces(this, getListView()).execute();
}
classGetPlacesextendsAsyncTask<Void, Void, Void>{
privateProgressDialog dialog;
privateContext context;
privateString[] placeName;
privateString[] imageUrl;
privateListView listView;
publicGetPlaces(Context context, ListView listView) {
// TODO Auto-generated constructor stubthis.context = context;
this.listView = listView;
}
@OverrideprotectedvoidonPostExecute(Void result) {
// TODO Auto-generated method stubsuper.onPostExecute(result);
dialog.dismiss();
listView.setAdapter(newArrayAdapter<String>(context, android.R.layout.simple_expandable_list_item_1,placeName));
}
@OverrideprotectedvoidonPreExecute() {
// TODO Auto-generated method stubsuper.onPreExecute();
dialog = newProgressDialog(context);
dialog.setCancelable(true);
dialog.setMessage("Loading..");
dialog.isIndeterminate();
dialog.show();
}
@OverrideprotectedVoiddoInBackground(Void... arg0) {
// TODO Auto-generated method stubPlacesService service = newPlacesService("paste your place api key");
List<Place> findPlaces = service.findPlaces(28.632808,77.218276,"hospital"); // hospiral for hospital// atm for ATM
placeName = newString[findPlaces.size()];
imageUrl = newString[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();
}
returnnull;
}
}
}
Solution 3:
Distance calculations are based on LatLong math such that:
Distance
This uses the ‘haversine’ formula to calculate great-circle distances between the two points – that is, the shortest distance over the earth’s surface – giving an ‘as-the-crow-flies’ distance between the points (ignoring any hills!).
Haversine formula:
R = earth’s radius (mean radius = 6,371km)
Δlat = lat2− lat1
Δlong = long2− long1
a = sin²(Δlat/2) + cos(lat1).cos(lat2).sin²(Δlong/2)
c = 2.atan2(√a, √(1−a))
d = R.c
(Note that angles need to be in radians to pass to trig functions). JavaScript:
var R = 6371; // kmvar dLat = (lat2-lat1).toRad();
var dLon = (lon2-lon1).toRad();
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) *
Math.sin(dLon/2) * Math.sin(dLon/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
Post a Comment for "How To Find Hospital Location Near By My Location?"