How To Find Location Near By My Current Location?
I need some idea about 'How to find Hospital,School,Restaurant near by my location' using android,How is possible?
Solution 1:
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=<key>private 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;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.android.code.R;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
/**
* @author dwivedi ji *
* */publicclassCheckInActivityextendsListActivity {
private String[] placeName;
private String[] imageUrl;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);
newGetPlaces(this,getListView()).execute();
}
classGetPlacesextendsAsyncTask<Void, Void, Void>{
Context context;
private ListView listView;
private ProgressDialog bar;
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);
bar.dismiss();
this.listView.setAdapter(newArrayAdapter<String>(context, android.R.layout.simple_list_item_1, placeName));
}
@OverrideprotectedvoidonPreExecute() {
// TODO Auto-generated method stubsuper.onPreExecute();
bar = newProgressDialog(context);
bar.setIndeterminate(true);
bar.setTitle("Loading");
bar.show();
}
@Overrideprotected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
findNearLocation();
returnnull;
}
}
publicvoidfindNearLocation() {
PlacesServiceservice=newPlacesService("past your key");
/*
Hear you should call the method find nearst place near to central park new delhi then we pass the lat and lang of central park. hear you can be pass you current location lat and lang.The third argument is used to set the specific place if you pass the atm the it will return the list of nearest atm list. if you want to get the every thing then you should be pass "" only
*//* hear you should be pass the you current location latitude and langitude, */
List<Place> findPlaces = service.findPlaces(28.632808,77.218276,"");
placeName = newString[findPlaces.size()];
imageUrl = newString[findPlaces.size()];
for (inti=0; i < findPlaces.size(); i++) {
PlaceplaceDetail= findPlaces.get(i);
placeDetail.getIcon();
System.out.println( placeDetail.getName());
placeName[i] =placeDetail.getName();
imageUrl[i] =placeDetail.getIcon();
}
}
}
Solution 2:
Use Google places api to find out near by hospital or what ever .....
Post a Comment for "How To Find Location Near By My Current Location?"