Unable to get city from lat,long values? - android

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.

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;
}
}

Find distance and time for nearby places from current location using Volley

I’m new with android studio. I want to use Volley to retrieve data from the Google server. I use two HTTP URL to search for nearby places and the distance and time from current location. However, I cannot find any method to use Volley for sending two “JSONRequest”. First request for searching nearby places and the second one for distance and time matrix for each nearby places from current location. The distance matrix HTTP URL is depend on the first one to find the nearby places before it can be executed. Is there any method to search nearby places with distance and time using single HTTP URL?
Or other method to use Volley for multiple request?
Your help is appreciated.
Here the Code I'm Using:
private int PROXIMITY_RADIUS = 5000; //5.0KM
private String NAME = "emergency";// emergency+hospital
private String TYPE = "hospital";
private String googlePlacesData;
private String googleDistData;
private Places placeJsonParser = new Places();
public List<PlaceData> searchNearby(double latitude, double longitude)
throws Exception{
List<PlaceData> googlePlacesList = new ArrayList<>();
try {
StringBuilder googlePlacesUrl = new StringBuilder("https://maps.googleapis.com/maps/api/place/nearbysearch/json?");
googlePlacesUrl.append("location=" + latitude + "," + longitude);
//googlePlacesUrl.append("&radius=" + PROXIMITY_RADIUS);
googlePlacesUrl.append("&rankby=distance");
googlePlacesUrl.append("&name=" + NAME);
googlePlacesUrl.append("&type=" + TYPE);
googlePlacesUrl.append("&sensor=true");
googlePlacesUrl.append("&key=" + GOOGLE_API_KEY);
if (googlePlacesUrl.length() > 0)
System.out.println("Exist GooglePlacesURL: " + googlePlacesUrl.toString());
Http http = new Http();
googlePlacesData = http.read(googlePlacesUrl.toString());
googlePlacesList = placeJsonParser.parse(new JSONObject(googlePlacesData));
}catch (Exception e){
e.printStackTrace();
}
return googlePlacesList;
}
public List<PlaceData> distanceMatrix(List<PlaceData> googlePlace,GPSTracker gps){
List<PlaceData> googlePlacesDistTimeList = new ArrayList<>();
try {
StringBuilder googleDistTimeURL = new StringBuilder("https://maps.googleapis.com/maps/api/distancematrix/json?");
googleDistTimeURL.append("origins=" + gps.getLatitude() + "," + gps.getLongitude());
googleDistTimeURL.append("&destinations=");
int counter = 0;
for (PlaceData place:googlePlace){
if (counter != (googlePlace.size()-1)){
googleDistTimeURL.append(place.getLatitude() + "," + place.getLongitude() + "|");
}else {
googleDistTimeURL.append(place.getLatitude() + "," + place.getLongitude());
}
counter++;
}
googleDistTimeURL.append("&key=" + GOOGLE_API_KEY);
if (googleDistTimeURL.length() > 0)
System.out.println("Exist GooglePlacesURL: " + googleDistTimeURL.toString());
Http http = new Http();
googleDistData = http.read(googleDistTimeURL.toString());
googlePlacesDistTimeList = placeJsonParser.parseDistTime(new JSONObject(googleDistData),googlePlace);
}catch (Exception e){
Log.e("Exception", e.toString());
}
return googlePlacesDistTimeList;
}

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();
}
}

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.

how to find longitude and latitude of particular location?

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;
}

Categories

Resources