Set Marker Icon On Google Maps V2 Android From Url
Solution 1:
Hi use a librariy that help you to view images from URL. visit https://github.com/nostra13/Android-Universal-Image-Loader for Android-Universal-Image-Loader
also you can visit to know for downloading the image
http://brainflush.wordpress.com/2009/11/23/droid-fu-part-2-webimageview-and-webgalleryadapter/
and after that
for the Custom Marker Icon, you can use an icon to show as a marker. load the icon from any kind of supported sources.
fromAsset(String assetName) – Loading from assets folder
fromBitmap (Bitmap image) – Loading bitmap image
fromFile (String path) – Loading from file
fromResource (int resourceId) – Loading from drawable resource
try as follows
// latitude and longitudedoublelatitude=17.385044;
doublelongitude=78.486671;
// create markerMarkerOptionsmarker=newMarkerOptions().position(newLatLng(latitude, longitude)).title("Hello Maps");
// Changing marker icon// set yours icon here
marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.my_marker_icon)));
// adding marker
googleMap.addMarker(marker);
Solution 2:
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);
}
}
});
}
Solution 3:
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.
Solution 4:
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 {
URLurl=newURL(link);
HttpURLConnectionconnection= (HttpURLConnection) url.openConnection();
try {
connection.connect();
} catch (Exception e) {
Log.v("asfwqeds", e.getMessage());
}
InputStreaminput= connection.getInputStream();
BitmapmyBitmap= BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
Log.v("asfwqeds", e.getMessage());
e.printStackTrace();
returnnull;
}
}
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.ThreadPolicypolicy=newStrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
Post a Comment for "Set Marker Icon On Google Maps V2 Android From Url"