Gilad Neiger
Gilad Neiger

Reputation: 643

Make a specific address to a Google Map Marker

I have a google map activity in my app. I want to enter a specific address of a place, and I want the app to make it to a Google Map Marker.

Until now, I've put a LatLng cod, Like this:

double location_left = Double.parseDouble(leftLocation);
double location_right = Double.parseDouble(rightLocation);
String place_title = child.child("place/place_title").getValue().toString();
LatLng cod = new LatLng(location_left, location_right);
googleMap.addMarker(new MarkerOptions().position(cod).title(place_title));

Is there any option of making a specific address like this "Covert St New york, United States" to a LatLng cod? or to a google maps marker?

Upvotes: 0

Views: 847

Answers (2)

Bharatesh
Bharatesh

Reputation: 9009

Yes using Geocoder API you can convert Address to LatLong List getFromLocationName (String locationName, int maxResults)

and Lat-Long to address List getFromLocation(double latitude, double longitude, int maxResults)

Upvotes: 1

Hitesh Sahu
Hitesh Sahu

Reputation: 45072

This function return Google Map image of input address you can change it to return LatLng

public static String getLocationURLFromAddress(Context context,
        String strAddress) {

    Geocoder coder = new Geocoder(context);
    List<android.location.Address> address;
    LatLng p1 = null;

    try {
        address = coder.getFromLocationName(strAddress, 5);
        if (address == null) {
            return null;
        }
        android.location.Address location = address.get(0);
        location.getLatitude();
        location.getLongitude();

        return "http://maps.googleapis.com/maps/api/staticmap?zoom=18&size=560x240&markers=size:mid|color:red|"
                + location.getLatitude()
                + ","
                + location.getLongitude()
                + "&sensor=false";

        //
        // p1 = new LatLng(location.getLatitude(), location.getLongitude());

    } catch (Exception ex) {

        ex.printStackTrace();
    }
    return strAddress;

    // return p1;
}

You can directly load image url of Address with the help of Picasso or Glide or if you want to make marker in Map you can do something like this :-

private void addMarker( LatLng currentLatLng ) {

            MarkerOptions options = new MarkerOptions();

            // following four lines requires 'Google Maps Android API Utility Library'
            // https://developers.google.com/maps/documentation/android/utility/
            // I have used this to display the time as title for location markers
            // you can safely comment the following four lines but for this info

            IconGenerator iconFactory = new IconGenerator(this);

            iconFactory.setStyle(IconGenerator.STYLE_PURPLE);
            options.icon(BitmapDescriptorFactory.fromBitmap(iconFactory.makeIcon(mLastUpdateTime)));
            options.anchor(iconFactory.getAnchorU(), iconFactory.getAnchorV());


            Marker mapMarker = googleMap.addMarker(options);

            long atTime = mCurrentLocation.getTime();

            mLastUpdateTime = DateFormat.getTimeInstance().format(new Date(atTime));

            mapMarker.setTitle(mLastUpdateTime);

            Log.d(TAG, "Marker added.............................");
            googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLatLng,
                    13));
            Log.d(TAG, "Zoom done.............................");
        }

Upvotes: 0

Related Questions