0

I have some locations from all over the city which I previously collect from the same app, but the case is when I receive this locations from the backend I need only to draw the locations are in my street, (I only need to see the locations that belongs only in my street or located only in my street I'm walking in right now)

Is there anyway to differentiate between the street Im walking in and the other streets round me, by get the street name more accurately or getting something like streetId`?

I'm calling this method at onLocationChanged method

private String getStreetName(Location location) {
    Geocoder geocoder;
    List<Address> addresses;
    String strAdd="";
    geocoder = new Geocoder(this, Locale.getDefault());

    try {
        addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
        if (!addresses.isEmpty()) {
            Address returnedAddress = addresses.get(0);
            StringBuilder strReturnedAddress = new StringBuilder("");
            int i = 0;
            do{
                strReturnedAddress.append(returnedAddress.getAddressLine(i)).append(" ");
                i++;
            }while (i < returnedAddress.getMaxAddressLineIndex());
            strAdd = strReturnedAddress.toString();
        } else {
            Log.e("MyCurrentLoctionAddress", "No Address returned!");
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    return strAdd;
}

The problem is this address that I get from geocode is no accurate at all.

I have got these three addresses at the same street within only 100 meter at this street, I got 3 deferent addresses

*Mashyakhet Al Azhar Al Sharif, El-Gamaleya, Monsha'et Nasser, Cairo Governorate, Egypt

*Salah Salem St, Monsha'et Nasser, Cairo Governorate, Egypt 

*139 Salah Salem St, El-Gamaleya, El Gamaliya, Cairo Governorate, Egypt
danial iranpour
  • 282
  • 1
  • 2
  • 11

1 Answers1

1

Because of GPS (and other location determination services too) errors you did not obtain "ground truth" location coordinates. So, IMHO, when you store location you need to map user current location to roads, that Google knows (e.g. like in this answer using Snap to Road part of Roads API) and store not only location on road, but distance from current user position, because you need to check location coordinates equivalence not only by coordinates itself, but taking into account some area around them. And then, when you need to check user position, again snap user path to road and map user position on that "snapped to road" path.

Or you can use Nearest Roads part of Roads API and select from Nearest Roads API call

https://roads.googleapis.com/v1/nearestRoads?points=60.170880,24.942795|60.170879,24.942796|60.170877,24.942796&key=YOUR_API_KEY

response:

{
  "snappedPoints": [
    {
      "location": {
        "latitude": 60.170877918672588,
        "longitude": 24.942699821922421
      },
      "originalIndex": 0,
      "placeId": "ChIJNX9BrM0LkkYRIM-cQg265e8"
    },
    {
      "location": {
        "latitude": 60.170876898776406,
        "longitude": 24.942699912064771
      },
      "originalIndex": 1,
      "placeId": "ChIJNX9BrM0LkkYRIM-cQg265e8"
    },
    {
      "location": {
        "latitude": 60.170874902634374,
        "longitude": 24.942700088491474
      },
      "originalIndex": 2,
      "placeId": "ChIJNX9BrM0LkkYRIM-cQg265e8"
    }
  ]
}

street, with minimal distance from user current location. To determine distance you can use SphericalUtil part of Maps SDK for Android Utility Library:

LatLng  userLocation = getUserLocation();
LatLng nearestStreet = null;
double minDist = 100000;
for (LatLng streetCoord: streetCoords) {
    double dist = SphericalUtil.computeDistanceBetween(userLocation, streetCoord);
    if (dist < minDist) {
        nearestStreet = streetCoord;
        dist = minDist;
    }
}

// here nearestStreet should contains location of nearest street (but may be null).

NB! Code above is just for illustration: you need to use geocoding to get street name for nearestStreet LatLng object or parse response into Place object and get PlaceID from it to get the street name etc.

Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79