I'm trying to find the location (lat/long) of a street address w/o city - such as "123 Main St." - closest to the current location. This functionality is built into the Google Maps app as well as the iOS maps api, so it's surprising to find it missing for Android - e.g. calling Geocoder.getFromLocation() and have the platform insert a reference point. I have tried several solutions, the following is the best, but still feels inferior.
I make calls to Geocoder.getFromLocationName() with lower-left and upper-right coord. Calls are made beginning with a 10kmx10km area around the current location, and are repeated (30x30, 100x100, and then without the bounding box parameters)until some Addresses are returned. When multiple addresses are returned, the closest is calculated and used:
UPDATE: This approach seemed like it would be inefficient for easily found addresses outside the bounds. E.g. "New york, NY" or "Boston" searched from the west coast - requiring 3 bounded and 1 unbounded call to Geocoder.getFromLocation(). However, unexpectidly, the correct lat/lng is returned for NYC and Boston, on the first call, with tightest bounds here in CA. Google is being smart and ignoring the bounds for us. This may cause problems for some, but it is great for this approach.
package com.puurbuy.android;
import java.io.IOException;
import java.util.List;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.os.AsyncTask;
import android.util.Log;
public class GeocoderRunner extends AsyncTask<String, Void, Address> {
final static double LON_DEG_PER_KM = 0.012682308180089;
final static double LAT_DEG_PER_KM =0.009009009009009;
final static double[] SEARCH_RANGES = {10, 50,800,-1}; //city, region, state, everywhere
private Context mContext;
private GeocoderListener mListener;
private Location mLocation;
public GeocoderRunner(Context context, Location location,
GeocoderListener addressLookupListener) {
mContext = context;
mLocation = location;
mListener = addressLookupListener;
}
#Override
protected Address doInBackground(String... params) {
Geocoder geocoder = new Geocoder(mContext);
List<Address> addresses = null;
//reference location TODO handle null
double lat = mLocation.getLatitude();
double lon = mLocation.getLongitude();
int i = 0;
try {
//loop through SEARCH_RANGES until addresses are returned
do{
//if range is -1, call getFromLocationName() without bounding box
if(SEARCH_RANGES[i] != -1){
//calculate bounding box
double lowerLeftLatitude = translateLat(lat,-SEARCH_RANGES[i]);
double lowerLeftLongitude = translateLon(lon,SEARCH_RANGES[i]);
double upperRightLatitude = translateLat(lat,SEARCH_RANGES[i]);
double upperRightLongitude = translateLon(lon,-SEARCH_RANGES[i]);
addresses = geocoder.getFromLocationName(params[0], 5, lowerLeftLatitude, lowerLeftLongitude, upperRightLatitude, upperRightLongitude);
} else {
//last resort, try unbounded call with 20 result
addresses = geocoder.getFromLocationName(params[0], 20);
}
i++;
}while((addresses == null || addresses.size() == 0) && i < SEARCH_RANGES.length );
} catch (IOException e) {
Log.i(this.getClass().getSimpleName(),"Gecoder lookup failed! " +e.getMessage());
}
if(addresses == null ||addresses.size() == 0)
return null;
//If multiple addresses were returned, find the closest
if(addresses.size() > 1){
Address closest = null;
for(Address address: addresses){
if(closest == null)
closest = address;
else
closest = getClosest(mLocation, closest,address);//returns the address that is closest to mLocation
}
return closest;
}else
return addresses.get(0);
}
#Override
protected void onPostExecute(Address address) {
if(address == null)
mListener.lookupFailed();
else
mListener.addressReceived(address);
}
//Listener callback
public interface GeocoderListener{
public void addressReceived(Address address);
public void lookupFailed();
}
//HELPER Methods
private static double translateLat(double lat, double dx){
if(lat > 0 )
return (lat + dx*LAT_DEG_PER_KM);
else
return (lat - dx*LAT_DEG_PER_KM);
}
private static double translateLon(double lon, double dy){
if(lon > 0 )
return (lon + dy*LON_DEG_PER_KM);
else
return (lon - dy*LON_DEG_PER_KM);
}
private static Address getClosest(Location ref, Address address1, Address address2){
double xO = ref.getLatitude();
double yO = ref.getLongitude();
double x1 = address1.getLatitude();
double y1 = address1.getLongitude();
double x2 = address2.getLatitude();
double y2 = address2.getLongitude();
double d1 = distance(xO,yO,x1,y1);
double d2 = distance(xO,yO,x2,y2);
if(d1 < d2)
return address1;
else
return address2;
}
private static double distance(double x1, double y1, double x2, double y2){
return Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) );
}
}
Perhaps this is the best solution, but I was wondering if there was a way to do this in a single call.
You code looks too complicated, here is much easier way:
String searchPattern = "123 Main St."
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//I use last known location, but here we can get real location
Location lastKnownLocation = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
List<Address> addresses = null;
try {
//trying to get all possible addresses by search pattern
addresses = (new Geocoder(this)).getFromLocationName(searchPattern, Integer.MAX_VALUE);
} catch (IOException e) {
}
if (addresses == null || lastKnownLocation == null) {
// location service unavailable or incorrect address
// so returns null
return null;
}
Address closest = null;
float closestDistance = Float.MAX_VALUE;
// look for address, closest to our location
for (Address adr : addresses) {
if (closest == null) {
closest = adr;
} else {
float[] result = new float[1];
Location.distanceBetween(lastKnownLocation.getLatitude(), lastKnownLocation.getLongitude(), adr.getLatitude(), adr.getLongitude(), result);
float distance = result[0];
if (distance < closestDistance) {
closest = adr;
closestDistance = distance;
}
}
}
return closest; //here can be null if we did not find any addresses by search pattern.
I tried Jin35's suggestion and increased the max_results of Geocoder.getFromLocationName(), but the results were not desirable. Firstly the large max_result, unbounded call took much longer (2.5x - 7x = 1 - 6 seconds) than the 5 result, geocoord bounded call on my emulator. Perhaps realworld would be faster and this factor becomes less significant.
The killer was no matter if the max_results were 50 or 100, only 20 results came back everytime. Seems Google is limiting the results on the server-side. The closest "123 Main St" was not amoung those 20 results for me - Tested from Mt View, CA and was returned Oakley, CA.
Unless there is another method other than Geocoder.getFromLocationName() for doing address lookup, or a better way to use bounding coord, I will accept my own original answer.
getFromLocationName(String locationName, int maxResults, double lowerLeftLatitude, double lowerLeftLongitude, double upperRightLatitude, double upperRightLongitude)
Related
I have a location marked in google map. The user has the option to change this location by dragging the map. I am using camera position to get the new location.Here is my code to get the original location:
mMap = googleMap
val addkey = intent.getStringExtra("address")
var addlocation = getLocationFromAddress(addkey) as LatLng
var mapLocation = CameraUpdateFactory.newLatLngZoom(addlocation, 18.0f)
mMap.animateCamera(mapLocation)
The getLocationFromAddress code is working fine.
The user then drags to a new location, clicks a button when done and upon confirmation the new location is accepted. I am trying to get the Latitude Longitude of the current camera position using the single line code:
addlocation = mMap.cameraPosition.target
But the code continues to return the Lat Long of old view. Where am I wrong?
Implement it like this -
private GoogleMap.OnCameraIdleListener onCameraIdleListener;
then use the camera listener to get the latitude longitude from the camera center position like this -
onCameraIdleListener = new GoogleMap.OnCameraIdleListener() {
#Override
public void onCameraIdle() {
LatLng latLng = mMap.getCameraPosition().target;
Geocoder geocoder = new Geocoder(MapsActivity.this);
try {
List<Address> addressList = geocoder.getFromLocation(latLng.latitude, latLng.longitude, 1);
if (addressList != null && addressList.size() > 0) {
String locality = addressList.get(0).getAddressLine(0);
String country = addressList.get(0).getCountryName();
if (!locality.isEmpty() && !country.isEmpty())
resutText.setText(locality + " " + country);
}
} catch (IOException e) {
e.printStackTrace();
}
}
};
Getting the address is an addition thing added here. Use it if you need it.
I am developing a bus tracking application where I am getting the location using service from server. Now, with that I want to show the bus movement and draw a proper polyline. I achieved a part of this but facing two main issues:
Every time bus marker is showing, it is not getting removed. So, the older footprint of the bus is still present. Although I reached destination, I am seeing many bus icons.
I am able to draw the polyline by joining the latitude and longitude but it is showing sometimes a straight line.
I have attached two screenshots for that.
The code which I used is here:
private void setmMap() {
progressDialog.show();
if (broadcastReceiver == null)
broadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("Testing", "inside the setmap");
// show progress dialog
try {
double latitude = intent.getDoubleExtra("lat", 22.560214);
double longitude = intent.getDoubleExtra("longi", 22.560214);
Log.d("SetMap", intent.getExtras().getString("time"));
LatLng startLocation = new LatLng(latitude, longitude);
m.setPosition(startLocation);
points.add(startLocation);
PolylineOptions options = new PolylineOptions().width(5).color(Color.BLUE).geodesic(true);
for (int i = 0; i < points.size(); i++) {
LatLng point = points.get(i);
options.add(point);
}
line = mMap.addPolyline(options); //add Polyline
mMap.moveCamera(CameraUpdateFactory.newLatLng(startLocation));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(startLocation, 15));
progressDialog.cancel();
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(context, Locale.getDefault());
addresses = geocoder.getFromLocation(latitude, longitude, 1);
String address = addresses.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
String city = addresses.get(0).getLocality();
String state = addresses.get(0).getAdminArea();
String country = addresses.get(0).getCountryName();
String postalCode = addresses.get(0).getPostalCode();
String strLoc = "Latitude:: "+latitude+" ::Longitude:: "+longitude+" ::Address:: "+address+" "+city+" "+
state;
Toast.makeText(getApplicationContext(),strLoc, Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
progressDialog.cancel();
}
}
};
registerReceiver(broadcastReceiver, new IntentFilter("carLocationService"));
}
Thanks,
Arindam.
1) you din't show part of code where marker m added, possible that code runs several times;
2) seems bus location sensor's polling period is quite large and does not allow tracking bus turns (bus can make several turns between it's known locations). So you need to interpolate path between known bus locations for example with Google Directions API.
I have a function in my application and when user clicks button, it will display the nearest office. Functionality works in part of Sweden but does not work in the north of Sweden. I use try-catch statement and I'll catch the null pointer exception. When I shut down my network and the location, I get the error message, but in north Sweden, there is no message, the app just crashes. My code is down there. onFindOfficeClick () is a single function and independent. How can I solve this problem? I tried debug but fails to hit the error ... do not know what to do. I take all coordinates from xml file. Does anybaody have any suggestions?
private void onFindOfficeClick() {
try {
LocationManager lm = (LocationManager)getSystemService(Service.LOCATION_SERVICE);
List<String> providersName = lm.getProviders(false);
if(providersName.size()>0){
Location location = lm.getLastKnownLocation(providersName.get(0));
float results [] = new float [1];
final double longitude = location.getLongitude();
final double latitude = location.getLatitude();
final int officeCount = mOfficesInfo.length;
double minDistance = 0;
int officeNum = 0;
for(int i=0; i<officeCount; i++){
Location.distanceBetween(latitude, longitude,
mOfficesInfo[i].getLatitude(), mOfficesInfo[i].getLongitude(), results);
if(i==0){
minDistance = results[0];
} else if(results[0]<minDistance){
minDistance=results[0];
officeNum = i;
}
}
int countryPosition = mCountries.indexOf(mOfficesInfo[officeNum].getCountry());
mCountrySpiner.setSelection(countryPosition);
mFindedOfficeName = mOfficesInfo[officeNum].getOfficeName();
mOfficesSpiner.setSelection(officeNum);
}
}
catch (NullPointerException e)
{
Toast.makeText(getApplicationContext(),"check your network and GP:s connections",Toast.LENGTH_SHORT).show();
}
}
put a null check before calling
LocationManager lm = (LocationManager)getSystemService(Service.LOCATION_SERVICE);
if(lm!=null){
lm.getProviders(false);.
}
and also might be
Location location = lm.getLastKnownLocation(providersName.get(0));
giving you null in the location variable so just do modify your code and put null check before getting latitude and laongitude from location.
if(location!=null){
final double longitude = location.getLongitude();
final double latitude = location.getLatitude();
}
becuase it is not good practice to catch NullPointerException in you code.
i have a situation where i need to use GPS technique.
i need to find the distance between two points when the person is walking.
When the person starts walking he will click on Start button and when he stops he clicks on stop button
after this it should show him
1.time taken
2.Distance travelled in Kms.
3.from where to where(places name) eg: a to b
Do i need to have google maps for this?
I saw the code here link to get the current location which gives me latitude longitude.
please help how to go with this
**
Edited:
**
This is the code i am using
private EditText editTextShowLocation;
private Button buttonGetLocation;
private ProgressBar progress;
private LocationManager locManager;
private LocationListener locListener = new MyLocationListener();
private boolean gps_enabled = false;
private boolean network_enabled = false;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
editTextShowLocation = (EditText) findViewById(R.id.editTextShowLocation);
progress = (ProgressBar) findViewById(R.id.progressBar1);
progress.setVisibility(View.GONE);
buttonGetLocation = (Button) findViewById(R.id.buttonGetLocation);
buttonGetLocation.setOnClickListener(this);
locManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
}
#Override
public void onClick(View v) {
progress.setVisibility(View.VISIBLE);
// exceptions will be thrown if provider is not permitted.
try {
gps_enabled = locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch (Exception ex) {
}
try {
network_enabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch (Exception ex) {
}
// don't start listeners if no provider is enabled
if (!gps_enabled && !network_enabled) {
AlertDialog.Builder builder = new Builder(this);
builder.setTitle("Attention!");
builder.setMessage("Sorry, location is not determined. Please enable location providers");
builder.setPositiveButton("OK", this);
builder.setNeutralButton("Cancel", this);
builder.create().show();
progress.setVisibility(View.GONE);
}
if (gps_enabled) {
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);
}
if (network_enabled) {
locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locListener);
}
}
class MyLocationListener implements LocationListener {
#Override
public void onLocationChanged(Location location) {
if (location != null) {
// This needs to stop getting the location data and save the battery power.
locManager.removeUpdates(locListener);
String londitude = "Londitude: " + location.getLongitude();
String latitude = "Latitude: " + location.getLatitude();
String altitiude = "Altitiude: " + location.getAltitude();
String accuracy = "Accuracy: " + location.getAccuracy();
String time = "Time: " + location.getTime();
editTextShowLocation.setText(londitude + "\n" + latitude + "\n" + altitiude + "\n" + accuracy + "\n" + time);
progress.setVisibility(View.GONE);
}
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
#Override
public void onClick(DialogInterface dialog, int which) {
if(which == DialogInterface.BUTTON_NEUTRAL){
editTextShowLocation.setText("Sorry, location is not determined. To fix this please enable location providers");
}else if (which == DialogInterface.BUTTON_POSITIVE) {
startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
}
It is showing the Logitude Latitude which i am entering from emulator control.
In this i am manually entering the details of longitude and latitude
by going to window->showview->other->emulator control for testing in the emulator
but what i need is i will have two edittext where i enter the place name(A) and (B)
it should give me the distance
please help
try using Google Distance Matrix Api
https://developers.google.com/maps/documentation/distancematrix/
You can use currentTimeinMillis() to get your start and end time for your journey.
You can then use the formulas explained here to find the distance and lastly you will have to use a reverse geocoding service such as Nominatim to be able to get the address of a place from your GPS coordinates.
That being said, the distance formula will get you the distance between one point and the next, not the actual displacement. If this is not what you need, but rather you want the actual distance travelled you will need to calculate this value at a shorter interval.
You can find out the distance between two locations(in terms of latitude and longitude) by making use of Spherical Trigonometry
Coming to time make use of simple date objects and compare the startTime and endTime.
(OR)
You can get approximate distance using below code
double distance;
Location locationA = new Location("point A");
locationA.setLatitude(latA);
locationA.setLongitude(lngA);
Location locationB = new Location("point B");
locationB.setLatitude(latB);
LocationB.setLongitude(lngB);
distance = locationA.distanceTo(locationB);
For getting the distance between 2points(A to B) there is a function called distanceTo in android.
For e.g.
double distance = startLocation.distanceTo(finishLocation)/1000;
For time taken as npinti said you can use currentTimeinMillis() or you can also use Timer and show it to user when he clicks on start button. Its just like stopwatch.
Edited
Place A - New York
Place B - Paris
In this case you first need to convert the string into Location(i.e you need latitude & longitude). For that you have use the concept of Geocoding.
List<Address> foundGeocode = null;
foundGeocode = new Geocoder(this).getFromLocationName("address here", 1);
foundGeocode.get(0).getLatitude(); //getting latitude
foundGeocode.get(0).getLongitude();//getting longitude
After that you can calculate the distance from the distanceTo method.
Hope this will help....
I suppose the question is one of walking..
Are you walking on the streets, or as the crow flies?
If it's streets, and your connected to the net, use google's api.. It calculates routing based on two points and returns XML.. Should be easy enough to figure out.
If it's crow flies.. well then, just do (a*a) + (b*b) = (c*c) this is by far the easier..
You could have your user tap for major turns.. Or you could keep a runnable running every 10 seconds from when they hit start, and plot the points. Still a*a+b*b=c*c but just a bunch of steps.
Of course you'd have to run it in a service.. And given the choice I'd go with that option. You could adjust the cycle time based on speed traveled. Faster would be smaller pauses.
It requires less on your dataplan.
EDIT
Ah.. I see what you're looking for. Tis not what I thought you were asking for.
Simplify.. convert lat/long to GPS. and then do simple math on the last point stored
void addDistance()
{
newX = ...
newY = ...
deltaX = absolute(m_oldX - newX)
deltaY = absolute(m_oldY = newY)
m_distance += sqrt(deltaX^2 + deltaY^2);
m_oldX = newX;
m_oldY = newY;
}
void startOver()
{
newX = ...
newY = ...
m_distance = 0;
m_oldX = newX;
m_oldY = newY;
}
I am using Android Geocoding to get the current city with Address.getLocality(). It has worked fine, until just recently it appears to often return null for the locality.
Here is an example:
try {
Geocoder c = new Geocoder(this, Locale.getDefault());
double lat = 51.481;
double lon = 0.0;
List<Address> l = c.getFromLocation(lat, lon, 5);
for (Address a: l) {
Log.i("GeocoderTest", "Locality " + a.getLocality() + " (" + a + ")");
}
} catch (IOException e) {
Log.e("GeocoderTest", "", e);
}
This now logs the following message for the first returned address:
Locality null (Address[addressLines=[0:"14-18 Park Vista",1:"London
Borough of Greenwich, London
SE10",2:"UK"],feature=,admin=null,sub-admin=null,locality=null,thoroughfare=Park
Vista,postalCode=null,countryCode=GB,countryName=United
Kingdom,hasLatitude=true,latitude=51.4819069,hasLongitude=true,longitude=-6.327E-4,phone=null,url=null,extras=null])
Some locations do return the city in the locality, while a location next to it does not.
So it did work just fine before, actually I had not seen a null locality before. So I guess something must have changed in Google's geocoding service. Any idea what is going on, and is this situation permanent? If yes, what would be the best way to determine the city from a location?
I noticed, that very often getLocality() returns null for the first address in the list, returned by Geocoder.
On the other hand correct city name stays at Locality of a next Address.
So I am using this workaround and it works well for big cities:
private String getCityNameByCoordinates(double lat, double lon) throws IOException {
List<Address> addresses = mGeocoder.getFromLocation(lat, lon, 10);
if (addresses != null && addresses.size() > 0) {
for (Address adr : addresses) {
if (adr.getLocality() != null && adr.getLocality().length() > 0) {
return adr.getLocality();
}
}
}
return null;
}
Now I live in Canada, Ontario, Hamilton (Hamilton is my city, Ontario is the province)
I noticed that getLocality() returns null, and getAdminArea() returns Ontario, and getSubLocality() returns Hamilton. ch
In Kotlin I have done something like this, given the address a
var place = a.locality
if (place == null) place = a.subLocality
if (place == null) place = a.subAdminArea
if (place == null) place = a.adminArea
It works even for remote places
Using getSubAdminArea() worked fine for me.