How to get the location of user after getting longitude and latitude - android

I am new to android mobile development. I have used the Location Manager class and successfully found out the Longitude and the Latitude of the user. I want to use these values to find the city name. I don't want maps, I just want to get the city name. How do I do this?

First get Latitude and Longitude using Location and LocationManager class(That you have completed). Now try the code below for Get the city,address info
double latitude = location.getLatitude();
double longitude = location.getLongitude();
Geocoder gc = new Geocoder(this, Locale.getDefault());
try {
List<Address> addresses = gc.getFromLocation(lat, lng, 1);
StringBuilder sb = new StringBuilder();
if (addresses.size() > 0) {
Address address = addresses.get(0);
for (int i = 0; i < address.getMaxAddressLineIndex(); i++)
sb.append(address.getAddressLine(i)).append("\n");
sb.append(address.getLocality()).append("\n");
sb.append(address.getPostalCode()).append("\n");
sb.append(address.getCountryName());
City info is now in sb. Now convert the sb to String (using sb.toString() ).

https://github.com/commonsguy/cw-lunchlist
https://github.com/commonsguy/cw-android
http://developer.android.com/reference/android/location/LocationManager.html
Have a look at these sites this will help you!!!!!1

You can use the Geocoder
Geocoder myLocation = new Geocoder(context, Locale.getDefault());
List<Address> myList = null;
try {
myList = myLocation.getFromLocation(latitude, longitude, 1);
} catch (IOException e) {}
Where longitude and latitude are the valued retrieved by networks or GPS

Related

Error in getting address of a location in android

based on this tutorial: http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/. I can now get latitude and longtitude based on location. Now I', trying to get the exact address with Geocoder: (Main is the class code above belongs to)
GPSTracker GPS = new GPSTracker(Main.this);
double latitude = GPS.getLatitude();
double longitude = GPS.getLongitude();
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(Main.this, Locale.getDefault());
addresses = geocoder.getFromLocation(latitude, longitude, 1);
String address = addresses.get(0).getAddressLine(0);
LogCat says:
07-06 11:29:41.911: W/System.err(1670): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
I wonder what am I doing wrong?
GPSTracker GPS = new GPSTracker(Main.this);
double latitude = GPS.getLatitude();
double longitude = GPS.getLongitude();
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(Main.this, Locale.getDefault());
addresses = geocoder.getFromLocation(latitude, longitude, 1);
if(addresses!=null && addresses.size()!=0){
String address = addresses.get(0).getAddressLine(0);
}
//if(addresses!=null && addresses.size()!=0)...check this portion..your address cannot be null and and the the size of array should not be zero...

Get Local Cities names using Google Map API

In my android app, I have to list all local cities names using my current location with Google Map API
Input
Current Co-ordinates
Current place Name.
Output
Local cities of current place.
You must collect the latitude and longitude first.
then you can user geocoder to get it. Like :-
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());
Try this way
public List<Address> getAddressListFromLatLong(double lat, double lng) {
Geocoder geocoder = new Geocoder(this);
List<Address> list = null;
try {
list = geocoder.getFromLocation(lat, lng, 20);
// 20 is no of address you want to fetch near by the given lat-long
for (Address address : list) {
System.out.println(address);
}
} catch (Throwable e) {
e.printStackTrace();
}
return list;
}

Distance of Location from a zip (postal) code

The Android API has Location.distanceBetween(), which accepts two lat/lon values and returns a distance in meters. Is there a way that I could get this distance with only having a zip (postal) code for one of my points?
You may want to use Android's Geocoder API. Something like this should work:
String locationName = zipCode + ", " + countryName;
Geocoder geoCoder = new Geocoder(context, Locale.getDefault());
try {
List<Address> address = geoCoder.getFromLocationName(locationName, 1);
double latitude = address.get(0).getLatitude();
double longitude = address.get(0).getLongitude();
Location.distanceBetween(...);
} catch (IOException e) {
e.printStackTrace();
}
You need to include the country's name because of this: Get latitude and longitude based on zip using Geocoder class in Android

Getting street name from Address/Location object in Android

I'm trying to get the street name of my current location but I can't seem to get it.
I use this method to retrieve the Address:
public Address getAddressForLocation(Context context, Location location) throws IOException {
if (location == null) {
return null;
}
double latitude = location.getLatitude();
double longitude = location.getLongitude();
int maxResults = 1;
Geocoder gc = new Geocoder(context, Locale.getDefault());
List<Address> addresses = gc.getFromLocation(latitude, longitude, maxResults);
if (addresses.size() == 1) {
return addresses.get(0);
} else {
return null;
}
}
And then I can do things like. address.getLocality() and address.getPostalCode()
But what I want is the street name. Like in "Potterstreet 12". When I print the AddressLine(0) and AddressLine(1) I only get the postalcode, city and country.
How can I retrieve the street name of the position i'm currently at?
Have you tried using getAddressLine ?
See here for more info on this method
Something like this should do (untested):
for (int i = 0; i < addresses.getMaxAddressLineIndex(); i++) {
Log.d("=Adress=",addresses.getAddressLine(i));
}
Try something like this in your code
String cityName=null;
Geocoder gcd = new Geocoder(getBaseContext(),Locale.getDefault());
List<Address> addresses;
try {
addresses = gcd.getFromLocation(location.getLatitude(), location
.getLongitude(), 1);
if (addresses.size() > 0)
StreetName=addresses.get(0).getThoroughfare();
String s = longitude+"\n"+latitude +
"\n\nMy Currrent Street is: "+StreetName;
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
it works for me :-) Good luck ;-)
If you have a complete address (city + street), in
address.getAddressLine(0)
you find the street name and number.
getFromLocation wasn't working for me either. There are a couple steps you can take.
1. First off go into gradle and make sure you are using the latest play services lib.
2. Don't over specify, the reason I got no results is because I had to much info in my address. When I removed the postal code I got results every time.
3. Try the online api:
http://maps.google.com/maps/api/geocode/json?address=192%20McEwan%20Dr%20E,%20Caledon,%20ON&sensor=false
Just replace the address in there with yours.
Good luck
I had a very similar problem but with the Country name, this is the function I ended up using:
function getCountry(results) {
var geocoderAddressComponent,addressComponentTypes,address;
for (var i in results) {
geocoderAddressComponent = results[i].address_components;
for (var j in geocoderAddressComponent) {
address = geocoderAddressComponent[j];
addressComponentTypes = geocoderAddressComponent[j].types;
for (var k in addressComponentTypes) {
if (addressComponentTypes[k] == 'country') {
return address.long_name;
}
}
}
}
return 'Unknown';
}
You should be able to adapt this to get the street name out without much fuss.
Inspired by this answer
Geocoder gcd = new Geocoder(this, Locale.getDefault());
List<Address> addresses =
gcd.getFromLocation(currentLatitude, currentLongitude,100);
if (addresses.size() > 0 && addresses != null) {
StringBuilder result = new StringBuilder();
myaddress.setText(addresses.get(0).getFeatureName()+"-"+addresses.get(0).getLocality()+"-"+addresses.get(0).getAdminArea()+"-"+addresses.get(0).getCountryName());
}
getfeaturename() return Streetname
getlocality() return city
getadminarea() return State
That's All..!

find out the current location

I want to dispaly the current location in my application not in map.
I want the current palce using current lattitude and longitude .
For Ex some 'x' person i want to know his location.but i want to know his location using his current lattitude and longitude.
When i use the below code it`Context context;
Geocoder geoCoder = new Geocoder(
getBaseContext(), Locale.getDefault());
List<android.location.Address> addresses = geoCoder.getFromLocation(17.385044,78.486671, 1);
String addr = "";
if (addresses.size() > 0)
{
for (int k=0; i<addresses.get(0).getMaxAddressLineIndex();
k++)
addr += addresses.get(0).getAddressLine(i) + "\n";
}
Toast.makeText(getBaseContext(), addr, Toast.LENGTH_SHORT).show();
Log.v("People","##############"+addr);
The "addr" does not getting any value.why it is happened here my Activity is extended by MapActivity and also tell me without extending activity (simply in class) how do you find the current location using current lattitude and longitude ?
please give me the code suggestions for this.Thanks in advance
Use the android.location package. There's a brief guide to using it here.
Use Geocoder class and call getFromLocation method.
This snippet Toast your current location starting from latitude and longitude:
Geocoder geocoder = new Geocoder(context, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
if (addresses.size() > 0)
Toast.makeText(getBaseContext(),addresses.get(0).getLocality());

Categories

Resources