TheDevMan
TheDevMan

Reputation: 5954

Figure out Latitude and Longitude

I am trying to develop an android application that can do task according to the lat/long. This is something like if I am in particular suburb (Lets say I am in Belconnen, ACT Australia, I would like to get the details of that place automatically) - if I move out the border of belconnen then I have to show some other details.

If you check this google maps link: http://goo.gl/4mItcF you would see the red border is only belconnen suburb.

My question is how do I give the borders in my App (meaning how do I tell my App that I am now in belconnen, ACT? Is it by getting lat/long along the borders store them in DB and check if I am inside required lat/long, if that is the case DB would have huge numbers only for Belconnen, ACT right?

Or is there an easier way to get the borders?

Let me know!

Thanks!

Upvotes: 2

Views: 679

Answers (5)

Prem
Prem

Reputation: 4821

As far as i understand, you would like to create a Location Aware Application.

As per the Android documentation, Google Play services location APIs are preferred over the Android framework location APIs (android.location) as a way of adding location awareness to your app.

Step 1 - Knowing the current Location of the User

Google recommends to use FusionProvider API for this purpose. It is one of the location APIs in Google Play services where you can specify requirements at a high level, like high accuracy or low power. It also optimizes the device's use of battery power.

https://developer.android.com/reference/com/google/android/gms/location/FusedLocationProviderApi.html

Step 2 - Based on the latitude and Longitude information retrieved in the location object, you can use the below Google web service to get the postal code of the location.

http://maps.googleapis.com/maps/api/geocode/json?latlng=lattitude,longitude&sensor=true

Step 3 - How Frequent can we retrieve the location information

Turning on the listeners continuously will impact the device battery highly. So, to avoid this, we need to minimize the frequency of retrieving the location information.

I would recommend to measure the distance traveled by the user and based on that set the next location update trigger. Since you are concerned about boundary only, we can set a high value based on the device acceleration. Refer the below links for more details on how to implement this effectively. https://stackoverflow.com/a/11644809/623517

Hope this helps.

Upvotes: 1

Bhavit S. Sengar
Bhavit S. Sengar

Reputation: 8924

There is a way , it is called Reverse Geocoding. You can use it by two ways :

1. Using Geocoder class of Android -

Geocoder gcd = new Geocoder(context, Locale.getDefault());
List<Address> addresses = gcd.getFromLocation(lat, lng, 1);
if (addresses.size() > 0) 
    System.out.println(addresses.get(0).getLocality());

Here you use lat = -35.2374551 and lng = 149.0672515 for Belconnen, ACT Australia. Check out the function getFromLocation for more information.

2. Using Google REST Web Services :

https://maps.googleapis.com/maps/api/geocode/json?latlng=-35.2374551,149.0672515 (JSON)

https://maps.googleapis.com/maps/api/geocode/xml?latlng=-35.2374551,149.0672515 (XML)

pass comma separated latitude and longitude and u will get the most precise location for that in response. Find more information here Reverse Geocoding google API

As soon as you go out of Belconnen, ACT Australia, the address in response will no longer contain this and you can put some logic to get your desired behavior.

Upvotes: 1

Deepak Kishore S
Deepak Kishore S

Reputation: 71

        LocationManager lm;
        lm=(LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
        Criteria c=new Criteria();
        String provider=lm.getBestProvider(c, false);
        Location l=lm.getLastKnownLocation(provider);
        double latitude = l.getLatitude(); // latitude
        double longitude = l.getLongitude(); // longitude

Upvotes: 0

dev.bmax
dev.bmax

Reputation: 10581

You can approximate the region with LatLngBounds (com.google.android.gms.maps.model.LatLngBounds). Use it like this:

LatLngBounds.Builder builder = new Builder();
for (int i=0; i<points.length; i++) {
    builder.include(points[i]);
}
LatLngBounds bounds = builder.build();

And then check if your location falls inside it:

if (bounds.contains(myLatLng)) {
    //your implementation
}

I know this approach cannot give perfect results but it seems to be more efficient then using the geofences.

Upvotes: 0

Karioki
Karioki

Reputation: 664

You can get full address from lat long and then search your city or location. This answer explain how to do it : How to get complete address from latitude and longitude?

If you want further flexibility you can use geofence : http://developer.android.com/training/location/geofencing.html

Upvotes: 0

Related Questions