How to find Hospital Location near by my location?

后端 未结 3 1589
独厮守ぢ
独厮守ぢ 2020-12-13 23:09

I wanna idea about \"How to find Hospital Location near by my location\" using android,How is possible?,can i use google\'s database for get latitude and longit

相关标签:
3条回答
  • 2020-12-13 23:14

    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

    Service

    on the place service

    enter image description here

    now select API Access tab and get the API KEY

    enter image description here

    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;
    
    
    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 + '}';
        }
    
    }
    

    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;
    
    
    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();
        }
    }
    

    Now create a new Activity where you want to get the list of nearest places.

    /** * */

        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;
            }
    
        }
    
    }
    
    0 讨论(0)
  • 2020-12-13 23:17

    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; // km
    var 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;
    
    0 讨论(0)
  • 2020-12-13 23:32

    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.

    0 讨论(0)
提交回复
热议问题