how to find longitude and latitude of particular location? - android

How to find Longitude and Latitude of Particular location?
the location is entered by user in edittext & click on search button then the location is display in googlemap.
i used following code for that but this is give error "Service not Available"
Geocoder geoCoder = new Geocoder(this, Locale.getDefault());
try {
address=geoCoder.getFromLocationName(txtlocation.getText().toString(), 1).get(0);
double longi=address.getLongitude();
double latit=address.getLatitude();
System.out.println("longitude:--- " +longi);
System.out.println("latitude:---" +latit);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(MapRouteActivity.this, e.toString(), Toast.LENGTH_LONG).show();
}

try to use
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); double longitude = location.getLongitude(); double latitude = location.getLatitude();
The call to getLastKnownLocation() doesn't block - which means it will return null if no position is currently available - so you probably want to have a look at passing a LocationListener to the requestLocationUpdates() method instead, which will give you asynchronous updates of your location.
private final LocationListener locationListener = new LocationListener() { public void onLocationChanged(Location location) { longitude = location.getLongitude(); latitude = location.getLatitude(); } }
lm.requestLocationUpdates(LocationManager.GPS, 2000, 10, locationListener);
You'll need to give your application the ACCESS_FINE_LOCATION permission if you want to use GPS.
You may also want to add the ACCESS_COARSE_LOCATION permission for when GPS isn't available and select your location provider with the getBestProvider() method.

try this code
public static String getLatLng(Context context,String addr){
Geocoder geocoder = new Geocoder(context, Locale.getDefault());
String add = "";
try{
List<Address> addresses = geocoder.getFromLocationName(addr, 5);
for(int i=0;i<1;i++){
Address obj = addresses.get(i);
for(int j=0;j<obj.getMaxAddressLineIndex();j++){
add = obj.getAddressLine(j);
add = add + "\nCountryName " + obj.getCountryName();
add = add + "\nCountryCode " + obj.getCountryCode();
add = add + "\nAdminArea " + obj.getAdminArea();
add = add + "\nPostalCode " + obj.getPostalCode();
add = add + "\nSubAdminArea " + obj.getSubAdminArea();
add = add + "\nFeatureName " + obj.getFeatureName();
add = add + "\nLocality " + obj.getLocality();
add = add + "\n" + obj.getSubThoroughfare();
add = add + "\nurl " + obj.getUrl();
add = add + "\nLatitude " + obj.getLatitude();
add = add + "\nLongitude " + obj.getLongitude();
}
add = add+"\n";
}
Log.v("IGA", "Address" + add);
}catch(Exception e){
e.printStackTrace();
add = e.toString();
}
return add;
}

Related

Getting some weird strings while i was trying to get location name using marker on touch event on maps

I'm trying to make an app to get the location name where the marker is placed on the map in android. When I used marker.position() function it gave me a weird string. Starts with, com.google.android.gms.maps.model.MarkerOptions#28f817d
I tried to use geocode but didn't work for me. I might be using it in an improper way.
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
#Override
public void onMapClick(LatLng point) {
mMap.clear();
MarkerOptions marker = new MarkerOptions();
mMap.addMarker(marker.position(point)
.draggable(true)
.title(String.valueOf(marker.position(point)))
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
.snippet(point.latitude + "," + point.longitude));
}
});
}
I wish that I can get the location name on the title.
Thats becouse marker.position(point)) returns an object of class LatLng and is not an String itself. You could use any of its properties (latitude & longitude):
String.valueOf(marker.position(point))
to
"" + marker.position(point).latitude + marker.position(point).longitude
Do something like this
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
#Override
public void onMapClick(LatLng point) {
mMap.clear();
MarkerOptions marker = new MarkerOptions();
mMap.addMarker(marker.position(point)
.draggable(true)
.title(getAddress(point.getLatitude(),point.getLongitude()))
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
.snippet(point.latitude + "," + point.longitude));
}
});
}
write a function called getAddress like this
public string getAddress(double lat, double lng) {
Geocoder geocoder = new Geocoder(HomeActivity.mContext, Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
Address obj = addresses.get(0);
String add = obj.getAddressLine(0);
add = add + "\n" + obj.getCountryName();
add = add + "\n" + obj.getCountryCode();
add = add + "\n" + obj.getAdminArea();
add = add + "\n" + obj.getPostalCode();
add = add + "\n" + obj.getSubAdminArea();
add = add + "\n" + obj.getLocality();
add = add + "\n" + obj.getSubThoroughfare();
Log.v("IGA", "Address" + add);
// Toast.makeText(this, "Address=>" + add,
// Toast.LENGTH_SHORT).show();
// TennisAppActivity.showDialog(add);
return add;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
return e;
}
}

Getting a place name in google map in android

I have a requirement that whenever I drag/browse the Google map and place a marker on it, it should display the name of the place(where the marker is placed) in an AutocompleteTextview. I have checked out the Places API and the Geocoder class but i least understood it. I need to do the task same as that of the Places tab in the life360 app. How can I achieve it?
try this it may help you...
When u mark a place, try to capture latitude & longitude with marker & use this code to get location name
public String getAddress(Context context, double lat, double lng) {
Geocoder geocoder = new Geocoder(context, Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
Address obj = addresses.get(0);
String add = obj.getAddressLine(0);
add = add + "\n" + obj.getCountryName();
add = add + "\n" + obj.getCountryCode();
add = add + "\n" + obj.getAdminArea();
add = add + "\n" + obj.getPostalCode();
add = add + "\n" + obj.getSubAdminArea();
add = add + "\n" + obj.getLocality();
add = add + "\n" + obj.getSubThoroughfare();
return add;
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
return null;
}
}
Try this I hope Its work.
public void getLocation(double lat, double lng) {
Geocoder geocoder = new Geocoder(MapsActivity.this, Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
Address obj = addresses.get(0);
String add = obj.getAddressLine(0);
Log.e("IGA", "Address" + add);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}

Searching markers from text in snippet

I need to add to my application a text box where I type address (street, city) and then app will displayed markers fulfilling this criteria. Any ideas or tutorials? My markers have adress written in snippet. Sorry for my poor english.
First pass address from text box to given method below which will give you latitude and longitude of that address and through this latitude and longitude you can place the marker on map.
//----Coding to get latitude and longitude from address----
private void searchFromLocationName(String name){
try {
Geocoder myGeocoder = new Geocoder(this);
List<Address> result = myGeocoder.getFromLocationName(name, 5);
if ((result == null)||(result.isEmpty()))
{
Toast.makeText(MapsActivity.this, "Sorry!No matches were found",Toast.LENGTH_LONG).show();
}
else{
String stringResult = "";
for (int i =0; i < result.size(); i++){
stringResult += "latitude: " + result.get(i).getLatitude() + "\n"
+ "longitude: " + result.get(i).getLongitude() + "\n";
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
And to show address on snippet:-
overlayItemsList.add(new OverlayItem(geoPoint, title, address));
Keep all your Markers in ArrayList<Marker> then loop over them and change visibility.
for (Marker marker : allMarkers) {
String snippet = marker.getSnippet();
boolean match = snippet.contains(myText);
marker.setVisible(match);
}

Get location name from fetched coordinates

What i have: currently my app is only telling me the coordinates of my current location.
What i want: Get location name from coordinates fetched by gps, so that i could know where exactly i am. (Name of location)
Here is complete code from fetching long - lat to getting address:
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
String provider = locationManager.getBestProvider(new Criteria(), true);
Location locations = locationManager.getLastKnownLocation(provider);
List<String> providerList = locationManager.getAllProviders();
if(null!=locations && null!=providerList && providerList.size()>0){
double longitude = locations.getLongitude();
double latitude = locations.getLatitude();
Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
try {
List<Address> listAddresses = geocoder.getFromLocation(latitude, longitude, 1);
if(null!=listAddresses&&listAddresses.size()>0){
String _Location = listAddresses.get(0).getAddressLine(0);
}
} catch (IOException e) {
e.printStackTrace();
}
}
You can us the GeoCoder which is available in android.location.Geocoder package. The JavaDocs gives u full explaination. The possible sample for u.
List<Address> list = geoCoder.getFromLocation(location
.getLatitude(), location.getLongitude(), 1);
if (list != null & list.size() > 0) {
Address address = list.get(0);
result = address.getLocality();
return result;
The result will return the name of the location.
Here i am given a single just pass the latitude and longitude in this function then you got all the information related to this latitude and longitude.
public void getAddress(double lat, double lng) {
Geocoder geocoder = new Geocoder(HomeActivity.mContext, Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
Address obj = addresses.get(0);
String add = obj.getAddressLine(0);
GUIStatics.currentAddress = obj.getSubAdminArea() + ","
+ obj.getAdminArea();
GUIStatics.latitude = obj.getLatitude();
GUIStatics.longitude = obj.getLongitude();
GUIStatics.currentCity= obj.getSubAdminArea();
GUIStatics.currentState= obj.getAdminArea();
add = add + "\n" + obj.getCountryName();
add = add + "\n" + obj.getCountryCode();
add = add + "\n" + obj.getAdminArea();
add = add + "\n" + obj.getPostalCode();
add = add + "\n" + obj.getSubAdminArea();
add = add + "\n" + obj.getLocality();
add = add + "\n" + obj.getSubThoroughfare();
Log.v("IGA", "Address" + add);
// Toast.makeText(this, "Address=>" + add,
// Toast.LENGTH_SHORT).show();
// TennisAppActivity.showDialog(add);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
I hope you get the solution to your answer.

Unable to get city from lat,long values?

Geocoder g = new Geocoder(this, Locale.getDefault());
java.util.List<android.location.Address> result = null;
// testing
try{
result = g.getFromLocation(43.324722,21.903333, 1);
if (result.size() > 0) {
selectedCity = result.get(0).getLocality();
}else
//no city found
}catch(Exception ex)
{
}
i am using above code to get city value in android but it always show no city found any one guide me what could be the problem?
public void getAddress(double lat, double lng) {
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
Address obj = addresses.get(0);
String add = obj.getAddressLine(0);
GUIStatics.currentAddress = obj.getSubAdminArea() + ","
+ obj.getAdminArea();
GUIStatics.latitude = obj.getLatitude();
GUIStatics.longitude = obj.getLongitude();
GUIStatics.currentCity= obj.getSubAdminArea();
GUIStatics.currentState= obj.getAdminArea();
add = add + "\n" + obj.getCountryName();
add = add + "\n" + obj.getCountryCode();
add = add + "\n" + obj.getAdminArea();
add = add + "\n" + obj.getPostalCode();
add = add + "\n" + obj.getSubAdminArea();
add = add + "\n" + obj.getLocality();
add = add + "\n" + obj.getSubThoroughfare();
Log.v("IGA", "Address" + add);
// Toast.makeText(this, "Address=>" + add,
// Toast.LENGTH_SHORT).show();
// TennisAppActivity.showDialog(add);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
I am using this function for getting all the information related to latitude and longitude just pass the latitude and longitude in this function then you find your answer.
I hope this is very help full to you.

Categories

Resources