3

I want to get details of the area I am standing in, I have the latitude and longitude using the LocationManager class, but how do I get the details such as name of the area, address etc? I don't want to use Geocoder due to restricted number of requests. How do find the details of the area using Google Places API?

mdanishs
  • 1,894
  • 8
  • 24
  • 46
  • This link might be helpful : http://stackoverflow.com/questions/2296377/how-to-get-city-name-from-latitude-and-longitude-coordinates-in-google-maps – yasin Nov 05 '16 at 21:43

4 Answers4

4

Use this class to retrieve your lon and lat if you still have problems with yours

public class GPSTracker implements LocationListener {

    private final Context mContext;

    // flag for GPS status
    boolean isGPSEnabled = false;

    // flag for network status
    boolean isNetworkEnabled = false;

    // flag for GPS status
    boolean canGetLocation = false;

    Location location; // location
    double latitude; // latitude
    double longitude; // longitude

    // The minimum distance to change Updates in meters
    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters

    // The minimum time between updates in milliseconds
    private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute

    // Declaring a Location Manager
    protected LocationManager locationManager;

    public GPSTracker(Context context) {
        this.mContext = context;
        getLocation();
    }

    public Location getLocation() {
        try {
            locationManager = (LocationManager) mContext
                    .getSystemService(LOCATION_SERVICE);

            // getting GPS status
            isGPSEnabled = locationManager
                    .isProviderEnabled(LocationManager.GPS_PROVIDER);

            // getting network status
            isNetworkEnabled = locationManager
                    .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

            if (!isGPSEnabled && !isNetworkEnabled) {
                // no network provider is enabled
            } else {
                this.canGetLocation = true;
                // First get location from Network Provider
                if (isNetworkEnabled) {
                    locationManager.requestLocationUpdates(
                            LocationManager.NETWORK_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("Network", "Network");
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
                // if GPS Enabled get lat/long using GPS Services
                if (isGPSEnabled) {
                    if (location == null) {
                        locationManager.requestLocationUpdates(
                                LocationManager.GPS_PROVIDER,
                                MIN_TIME_BW_UPDATES,
                                MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                        Log.d("GPS Enabled", "GPS Enabled");
                        if (locationManager != null) {
                            location = locationManager
                                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                            if (location != null) {
                                latitude = location.getLatitude();
                                longitude = location.getLongitude();
                            }
                        }
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return location;
    }

    /**
     * Stop using GPS listener
     * Calling this function will stop using GPS in your app
     * */
    public void stopUsingGPS(){
        if(locationManager != null){
            locationManager.removeUpdates(GPSTracker.this);
        }
    }

    /**
     * Function to get latitude
     * */
    public double getLatitude(){
        if(location != null){
            latitude = location.getLatitude();
        }

        // return latitude
        return latitude;
    }

    /**
     * Function to get longitude
     * */
    public double getLongitude(){
        if(location != null){
            longitude = location.getLongitude();
        }

        // return longitude
        return longitude;
    }

    /**
     * Function to check GPS/wifi enabled
     * @return boolean
     * */
    public boolean canGetLocation() {
        return this.canGetLocation;
    }


    public void onLocationChanged(Location location) {
    viewManager(location);
    }


    public void onProviderDisabled(String provider) {
    }


    public void onProviderEnabled(String provider) {
    }


    public void onStatusChanged(String provider, int status, Bundle extras) {
    }


}

Details when you get the location

private void viewManager(Location loc) {

      Toast.makeText(getBaseContext(),
            "Location changed : Lat: " + loc.getLatitude() + " Lng: " + loc.getLongitude(),
            Toast.LENGTH_SHORT).show();
      String longitude = "Longitude: " + loc.getLongitude();
      Log.v(TAG, longitude);
      String latitude = "Latitude: " + loc.getLatitude();
      Log.v(TAG, latitude);

      /*----------to get City-Name from coordinates ------------- */
      StringBuffer address = new StringBuffer();
      Geocoder gcd = new Geocoder(getBaseContext(), Locale.getDefault());
      List<Address> addresses;
      try {
         addresses = gcd.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1);

         if (addresses.size() > 0)
            System.out.println(addresses.get(0).getLocality());
         address.append(addresses.get(0).getAddressLine(0)).append("\n")
               .append(addresses.get(0).getAddressLine(1)).append("\n")
               .append(addresses.get(0).getAddressLine(2));

      } catch (IOException e) {
         e.printStackTrace();
      }

      String s = longitude + "\n" + latitude + "\n\nMy Currrent City is: \n" + address.toString();

   }
Festus Tamakloe
  • 11,241
  • 9
  • 50
  • 65
0

I think this post can help you: Using Google Places API

And here is the API documentation: https://developers.google.com/places/documentation/

You can also use foursquare venues API. It's very simple to use! https://developer.foursquare.com/docs/venues/venues

Community
  • 1
  • 1
Aerilys
  • 1,620
  • 1
  • 16
  • 21
0

Store the result of getFromLocation(double, double, int) method in an Address object, then call the method getLocality().

yasin
  • 252
  • 4
  • 6