Distance between user's location and a location entered by user - android

I want to find the distance between the location of the user and a place he enters. The entered location will be an address. Using location class I can find the coordinates of the user's location but how can I find those of the entered location. Couldn't find that in Google Maps API as well.

You can retrieve the location from an address like this:
public GeoPoint getLocationFromAddress(String strAddress) {
Geocoder coder = new Geocoder(this);
List<Address> address = new ArrayList<Address>();
try {
address = coder.getFromLocationName(strAddress,5);
if (address == null) {
return null;
}
Address location = address.get(0);
location.getLatitude();
location.getLongitude();
GeoPoint p1 = new GeoPoint((int) (location.getLatitude() * 1E6),
(int) (location.getLongitude() * 1E6));
return p1;
} catch (Exception e) {}
return null;
}
And for the distance use distanceBetween.

Related

Getting LatLng from adress

I am developing an android application and in that application, I want to search for places on a map without using places.api. is there a way to get LatLng from a given address?
You can get address from Latng by using Geocoder,please check below code
public GeoPoint getLocationFromAddress(String strAddress){
Geocoder coder = new Geocoder(this);
List<Address> address;
GeoPoint p1 = null;
try {
address = coder.getFromLocationName(strAddress,5);
if (address==null) {
return null;
}
Address location=address.get(0);
location.getLatitude();
location.getLongitude();
p1 = new G`enter code here`eoPoint((double) (location.getLatitude() * 1E6),
(double) (location.getLongitude() * 1E6));
return p1;
}
}

When find the latitude and longitude from address, how to give the address?

I follow this question (How can I find the latitude and longitude from address?) and try to get the latitude and longitude of an address. I call the method like this "GeoPoint point=getLocationFromAddress("colombo,sri lanka");" . but give null value. how to call method correctly??
public GeoPoint getLocationFromAddress(String strAddress){
Geocoder coder = new Geocoder(this);
List<Address> address;
GeoPoint p1 = null;
try {
address = coder.getFromLocationName(strAddress,5);
if (address==null) {
return null;
}
Address location=address.get(0);
location.getLatitude();
location.getLongitude();
p1 = new GeoPoint((int) (location.getLatitude() * 1E6),
(int) (location.getLongitude() * 1E6));
return p1;
}
}
In my opinion you should use google place autocomplete/place picker api's.
Select the place and thereafter you can get Latitude and Longitude.
Check this out:
https://developers.google.com/places/android-api/autocomplete
try below code
Geocoder gcd = new Geocoder(ReserveTimer.this, Locale.getDefault());
List<Address> addresses = gcd.getFromLocation(Double.parseDouble(lati), Double.parseDouble(longi), 1);
if (addresses.size() > 0) {
System.out.println(addresses.get(0).getLocality());
System.out.println(addresses.get(0).getCountryName());
System.out.println(addresses.get(0).getAddressLine(0));
System.out.println(addresses.get(0).getAdminArea());
System.out.println(addresses.get(0).getCountryName());
System.out.println(addresses.get(0).getPostalCode());
}
You can use the following method that take a Location object that contain latitud and longitud coordinates and Log the location to the LogCat console:
public void setLocation(Location loc) {
//Get Human readable address from latitud and longitud coordinates
if (loc.getLatitude() != 0.0 && loc.getLongitude() != 0.0) {
try {
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> list = geocoder.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1);
if (!list.isEmpty()) {
Address address = list.get(0);
Log.d(CLASS_TAG, "Mi direcci—n es: \n" + address.getAddressLine(0));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

Location within 100 km using Google Maps within Android

I am working on an Android application and Google Maps in which I am searching for the particular address by using the following code. I want to get the location of the searched address if it is in 100km within my location, and if it outside that limit, it will not show me.
I am trying to get the searched location within my 100km radius.
private class GeocoderTask extends AsyncTask<String, Void, List<Address>>{
#Override
protected List<Address> doInBackground(String... locationName) {
// Creating an instance of Geocoder class
Geocoder geocoder = new Geocoder(getBaseContext());
List<Address> addresses = null;
try {
//
//24.798406, 54.790448
//25.452403, 55.537519
// Getting a maximum of 3 Address that matches the input text
addresses = geocoder.getFromLocationName(locationName[0], 10,
24.861969, 54.857740,25.545368, 55.474347);//.getFromLocationName(locationName[0], 3);
} catch (IOException e) {
e.printStackTrace();
}
return addresses;
}
#Override
protected void onPostExecute(List<Address> addresses) {
if(addresses==null || addresses.size()==0){
Toast.makeText(getBaseContext(), "No Location found", Toast.LENGTH_SHORT).show();
}
// Clears all the existing markers on the map
mMap.clear();
// Adding Markers on Google Map for each matching address
for(int i=0;i<addresses.size();i++){
Address address = (Address) addresses.get(i);
LatLng latLng;
// Creating an instance of GeoPoint, to display in Google Map
latLng = new LatLng(address.getLatitude(), address.getLongitude());
String addressText = String.format("%s, %s",
address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",
address.getCountryName());
markerOptions = new MarkerOptions();
markerOptions.position(latLng);
// markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.flag));
markerOptions.title(addressText);
mMap.addMarker(markerOptions);
// Locate the first location
if(i==0)
mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
}
}
}
You can calculate distance using this code; here, distance is in meters. You can call this function and check where the location is in the range or not.
private boolean checkForArea(int rad, LatLng fromPosition, LatLng toPosition) {
Location locationA = new Location("point A");
locationA.setLatitude(fromPosition.latitude);
locationA.setLongitude(fromPosition.longitude);
Location locationB = new Location("point B");
locationB.setLatitude(toPosition.latitude);
locationB.setLongitude(toPosition.longitude);
int distance = (int) locationA.distanceTo(locationB);
if (distance / 1000 <= rad)
return true;
else
return false;
}

Can not resolve symbol GeoPoint in android studio

I am using android studio 0.8.9. i am using google map v2 and google services 5.2.08.i want to display marker from given address and also generated SHA Key using key-tool also enabled and get my API Key from google console. But when i am using GeoPoint for getting Lat and Long android studio does not recognize GeoPoint class.
My Java Activity code:
public class WelcomeUser extends ActionBarActivity {
GoogleMap googleMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome_user);
try {
if(null == googleMap){
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.mapView)).getMap();
/**
* If the map is still null after attempted initialisation,
* show an error to the user
*/
if(null == googleMap) {
Toast.makeText(getApplicationContext(),
"Error creating map", Toast.LENGTH_SHORT).show();
}
}
} catch (NullPointerException exception){
Log.e("mapApp", exception.toString());
}
double lat= 0.0, lng= 0.0;
String address = "Ahmedabad, Gujarat";
Geocoder geoCoder = new Geocoder(this, Locale.getDefault());
try
{
List<Address> addresses = geoCoder.getFromLocationName(address , 1);
if (addresses.size() > 0)
{
GeoPoint p = new GeoPoint(
(int) (addresses.get(0).getLatitude() * 1E6),
(int) (addresses.get(0).getLongitude() * 1E6));
lat=p.getLatitudeE6()/1E6;
lng=p.getLongitudeE6()/1E6;
Log.d("Latitude", ""+lat);
Log.d("Longitude", ""+lng);
}
}
catch(Exception e)
{
e.printStackTrace();
}
if(null != googleMap){
googleMap.addMarker(new MarkerOptions()
.position(p)
.title("Marker")
.draggable(true)
);
}
}
Please help me guys.Thanks in advance
Geocoder no longer provided latitude-longitude from addresses,use Reverse Geocoding API to get latitude-longitude from address.
Check Example here : Get latitude,longitude from address in Android

How to zoom to a country by name in an Android application

How to zoom to a country by name in an Android application(google map)? IF I get a coutry name Ireland then the map location to the Ireland
1 Get location by country name
Geocoder geocoder = new Geocoder(<your context>);
List<Address> addresses;
addresses = geocoder.getFromLocationName(<String address>, 1);
if(addresses.size() > 0) {
double latitude= addresses.get(0).getLatitude();
double longitude= addresses.get(0).getLongitude();
}
2 Zoom to current location
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lng), 10));
You can use the Geocoder API to fetch the location based on the country name and then animate the map camera to that position:
try {
List<Address> address = new Geocoder(this).getFromLocationName("Ireland", 1);
if (address == null) {
Log.e(TAG, "Not found");
} else {
Address loc = address.get(0);
Log.e(TAG, loc.getLatitude() + " " + loc.getLongitude());
LatLng pos = new LatLng(loc.getLatitude(), loc.getLongitude())
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(pos, 14));
}
} catch (IOException e) {
e.printStackTrace();
}

Categories

Resources