lschlessinger
lschlessinger

Reputation: 1954

Set marker icon on Google Maps v2 Android from URL

I am unable to add the icon provided by the Google Places API to the marker. For example, I would like to have the icon returned from the JSON of Google Places API:

"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png"

Here's how I am doing it now without adding the icon:

for (Place place : mNearPlaces.results) {
     LatLng placeLatLng = new LatLng(place.geometry.location.lat,
         map.addMarker(new MarkerOptions()
             .position(placeLatLng)
             .title(place.name));
}

I looked at this post and the answer suggested using some AsyncTask to download the image. Is there any other way to do this (that way seems slow)? Is there a standard way of setting the marker image provided by the Google Places API?

Here is the Google Maps Android API v2 Documentation for Markers

Thanks

Upvotes: 2

Views: 18936

Answers (3)

Faizan Haidar Khan
Faizan Haidar Khan

Reputation: 1215

While calling .icon() do the following

 protected Marker createMarker(double latitude, double longitude, String title, String snippet, String imageUrl) {

    MarkerOptions markerOptions = new MarkerOptions();
    return googleMap.addMarker(markerOptions
            .position(new LatLng(latitude, longitude))
            .anchor(0.5f, 0.5f)
            .title(title)
            .snippet(snippet)
            .icon(BitmapDescriptorFactory.fromBitmap(getBitmapFromLink(imageUrl))));
}

This method will convert the image url to bitmap

   public Bitmap getBitmapFromLink(String link) {
    try {
        URL url = new URL(link);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        try {
            connection.connect();
        } catch (Exception e) {
            Log.v("asfwqeds", e.getMessage());
        }
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
    } catch (IOException e) {
        Log.v("asfwqeds", e.getMessage());
        e.printStackTrace();
        return null;
    }
}

Now, an exception will occur in connection.connect() saying android.os.NetworkOnMainThreadException. To handle this Exception add this code in you oncreate or before adding the marker

if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }

Upvotes: 0

You can load url image to a GoogleMap mapMarker using following code. First set other properties like title, snippet, position etc....

  mapMarker= mGoogleMap.addMarker(new MarkerOptions()
                    .title(Integer.toString(total_steps))
                    .snippet("Steps")
                    .position(new LatLng(current_lat,current_longi)));
            mapMarker.setTag("current_position");
            mapMarker.showInfoWindow();
            loadMarkerIcon(mapMarker);

After use a custom method to load a image for icon property. You can add Glide gradle link to project.

private void loadMarkerIcon(final Marker marker) {
        String burlImg = "Url_imagePath;
        Glide.with(this).load(burlImg)
                .asBitmap().fitCenter().into(new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(Bitmap bitmap, GlideAnimation<? super Bitmap> glideAnimation) {

                if(bitmap!=null){
                  //  Bitmap circularBitmap = getRoundedCornerBitmap(bitmap, 150);
                    Bitmap mBitmap = getCircularBitmap(bitmap);
                    mBitmap = addBorderToCircularBitmap(mBitmap, 2, Color.WHITE,squareBitmapWidth);
                    BitmapDescriptor icon = BitmapDescriptorFactory.fromBitmap(mBitmap);
                    marker.setIcon(icon);
                }

            }
        });

    }

Upvotes: 4

Coderji
Coderji

Reputation: 7745

there are a lot of libraries that help you view images from URL like: URLImageViewhelper unfortunately, you can't load images from URL without AsyncTask however all the libraries that help you view images from URL are using Asynctask so only you have to do is call the function.

the implementation of the library class are available within the github page.

Upvotes: 1

Related Questions