2

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!

TheDevMan
  • 5,914
  • 12
  • 74
  • 144

5 Answers5

1

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.

Bhavit S. Sengar
  • 8,794
  • 6
  • 24
  • 34
  • Your 2 point requires internet. I am looking for solution that can work even if internet is not working. – TheDevMan Jan 06 '15 at 09:39
1

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.

Community
  • 1
  • 1
Prem
  • 4,823
  • 4
  • 31
  • 63
  • I don't check the location always. This is done only when user opens my APP. I have few important details stored in local DB. All I need is when I open the APP according to location I want to put the details and show it. For which I need to know the location and it borders? GEO FENCING works it good for small places but how can I give it big places? – TheDevMan Jan 08 '15 at 01:48
0

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

Community
  • 1
  • 1
Karioki
  • 646
  • 6
  • 18
  • Address format is perfect idea which has already been implemeneted but what if i don't have internet i would like show limited data. – TheDevMan Dec 23 '14 at 01:23
  • For Geofencing: I have feed the lat and long to compare my current lat/long for which I will have huge sets of lat/long only for the belconnen, Australia right? – TheDevMan Dec 23 '14 at 01:25
  • In geofencing you basically provide a circle and rest is handled by android, You don't have to compare yourself. For offline stuff you have to give try as i don.t know. – Karioki Dec 23 '14 at 07:46
0

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.

dev.bmax
  • 8,998
  • 3
  • 30
  • 41
0
        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