Question

Ok, so what I'm trying to do is have an EditText box in which the user can enter a street name. My application will then get the lat/long of that street and place a marker at that geopoint location on a map (Using the Google Maps API v2). So far I Have the EditText box set up as well as a map with a function to manually add markers at specified geopoints.

Example: If the user enters "Grafton Street" in the EditText box, the application will get the lat/long (53.342094,-6.259868) and subsequently place a marker at that point on the map using the GeoPoint function, or maybe even highlight the whole road (as seen in Google directions etc).

My problem is that I'm unsure how to actually get the latitude and longitude of the entered location to pass into my addMarker function. Any ideas or suggestions would be greatly appreciated!

EDIT: Solved, see my answer below.

Was it helpful?

Solution 2

First, you will need to use GoeCoder to get name and convert it to Lat\Long:

http://developer.android.com/reference/android/location/Geocoder.html

Then you will use GeoPoint to get a lat\long as an argument, and convert it to a a GeoPoint (for display on a Map).

https://developers.google.com/maps/documentation/android/v1/reference/com/google/android/maps/GeoPoint

OTHER TIPS

I've used the answers provided below and managed to get it working. I created a new LatLong rather than a GeoPoint simply because the addMarkerHere function uses that type. Here is the code I've used for anyone who may need it in the future:

Geocoder g = new Geocoder(this);
    List<Address> addressList = null;
    String searchRoad = "Insert Road Name Here!";
    try {
        addressList = g.getFromLocationName(searchRoad, 1);

    } catch (IOException e) {
        Toast.makeText(this, "Location not found",     Toast.LENGTH_SHORT)
                    .show();
            e.printStackTrace();

    } finally {
        Address address = addressList.get(0);

        if (address.hasLatitude() && address.hasLongitude()) {
            double selectedLat = address.getLatitude();
            double selectedLng = address.getLongitude();
            LatLng Road = new LatLng(selectedLat, selectedLng);
            Marker Custom = map.addMarker(new MarkerOptions()
                    .position(searchedRoad).title("Here is the road location")
                    .snippet("Hon the lads"));
    }

It called geocoding. You submit a request, they return a list of place that matched. Parsing that results, get the geometry of 1st place in that list.

Your simplified explanation is a good idea and a good application project.

But even if your brief explanation is very simple, it is not easy to give you the answer of which you want to get because it is an application project. You must collect many sample codes and documents through Google searching.

First, please see at this Android official manual.

Second, please download the sample code at the documents above.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top