2

This is sort of an extension to another question I found on here: Finding nearest street given a lat-long location

The OP asked for the nearest street name and the answer giving is not working for me. I need the street name for my app I live in Ireland and all our roads are named with a letter followed by the roads number. The letter is either L, R, N or M based on the road class. This is the information I require for my app.

Using the geocoder class to reverse geocode these co-ords: 53.286288,-6.801891 (which is right on a "R" road) gives me the area name but not the road name. On google maps if you enter the co-ords it says it is "R403".

My question is how do I get the nearest road name using co-ords with google api's? If the online google maps can report the road name is there a way I can?

I have looked into the Directions API and I am also wondering if I provide that with the above co-ords as a starting point and a destination point (because my app will only have the current location and there will be no destination) will the API return the road name?

Community
  • 1
  • 1
user2269886
  • 45
  • 1
  • 8
  • *Edit* Should also add the area name it returns for those co-ords is "Graigues" and this is the first element "[0]" in the returned Address object – user2269886 Jan 08 '14 at 17:16
  • Post some code please. It will be slightly different on Android/native platforms then with web services. – cbrulak Jan 08 '14 at 17:32
  • Edit your question and format it properly please. See: http://stackoverflow.com/about – cbrulak Jan 08 '14 at 17:49
  • public String getAddressForLocation(Context context, double lat, double lng) throws IOException { Geocoder myLocation = new Geocoder(activity.getApplicationContext()); List
    myList = myLocation.getFromLocation(lat, lng, 1); return myList.get(0).toString(); }
    – user2269886 Jan 08 '14 at 17:56
  • Sorry I'm new here, tried everything, the link you gave doesn't say how exactly. Had a google and theres no "{}" code button for me and I tried blocks, 4 space indents, console character, highlight and ctrl-k. dont know man sorry? – user2269886 Jan 08 '14 at 17:58
  • I looked on google maps at these coordinates, and there is a house right off the road. I really hope you didn't just give the entire internet your address. – BobbyD17 Jan 08 '14 at 18:14
  • i'd not use Geocoder because it got bad bugs and stops working too often to ignore this. And the known solution is to reboot the device. use Direction API – Marcin Orlowski Jan 08 '14 at 18:14
  • BobbyD17, haha no not at all I just picked a random road that was far from others in its class – user2269886 Jan 08 '14 at 18:43

1 Answers1

6

with Direction API you can acheive this. I don't use often this API, but you can get what you want using this :

http://maps.googleapis.com/maps/api/directions/json?origin=53.286288,-6.801891&destination=51,19&sensor=false

I put your coordinates in origin. And in the result you can see a start_address :

start_address: "R403, Co. Kildare, Irlande",

or without putting a destination :

http://maps.googleapis.com/maps/api/geocode/json?latlng=53.286288,-6.801891&sensor=false

formatted_address: "R403, Co. Kildare, Irlande",

EDIT : To call & retrieve the Json

Don't forget to ask Internet permission in the manifest

<manifest xlmns:android...>
 ...
 <uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>

here a method to get what you want with the lat & lng params. Don't forget to call this in a background thread. (You can do it easily with an AsyncTask).

private void getRoadName(String lat, String lng) {

    String roadName = "";
    String url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=__LAT__,__LNG__&sensor=false";

    url = url.replaceAll("__LAT__", lat);
    url = url.replaceAll("__LNG__", lng);

    DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
    HttpGet httpget = new HttpGet(url);

    InputStream inputStream = null;
    String result = null;
    try {
        HttpResponse response = httpclient.execute(httpget);           
        HttpEntity entity = response.getEntity();

        inputStream = entity.getContent();
        // json is UTF-8 by default
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
        StringBuilder sb = new StringBuilder();

        String line = null;
        while ((line = reader.readLine()) != null)
        {
            sb.append(line + "\n");
        }
        result = sb.toString();
    } catch (Exception e) { 
        // Oops
    }
    finally {
        try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
    }        

    JSONObject jObject = new JSONObject(result);
    JSONArray jArray = jObject.getJSONArray("results");
    if (jArray != null && jArray.size > 0) {
        try {
            JSONObject oneObject = jArray.getJSONObject(i);
            // Pulling items from the array
            roadName = oneObject.getString("formatted_address");
        } catch (JSONException e) {
        // Oops
        }
    }
}

code to parse JSON from : How to parse JSON in Android

Community
  • 1
  • 1
Andros
  • 4,069
  • 1
  • 22
  • 30
  • That seems like exactly what I need thanks. Can you also provide code as to how I would request that in my app? Permissions are set as my app also posts data to my server using httprequest but how do I receive? I would replace the hardcoded lat, lngs with variables like this: "http://maps.googleapis.com/maps/api/directions/json?origin="+lat+","+lng+"&destination=51,19&sensor=false" Just what method and/or technique should I use? Thanks – user2269886 Jan 08 '14 at 18:31