Updated 9 June 2018
In this blog, you know how to add URL image marker in Google Map.
It is a tough task to add URL Image to Marker. I use Picasso Library to parse image, for getting more about Picasso library see my other blog. Before knowing to add a marker in Map. You should check In your Manifest file having google map API.
1 2 3 |
<meta-data android:name="com.google.android.geo.API_KEY" android:value="@string/google_maps_key" /> |
<fragment>
element to your activity’s layout file,activity_maps.xml
This element defines aSupportMapFragment
to act as a container for the map and to provide access to the objectGoogleMap
. The tutorial uses the Android support library version of the map fragment, to ensure backward compatibility with earlier versions of the Android framework.
1 2 3 4 5 6 7 |
<fragment xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/map" android:name="com.google.android.gms.maps.SupportMapFragment" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.mapwithmarker.MapsMarkerActivity" /> |
2. In your activity’s methodonCreate()
, set the layout file as the content view. Get a handle to the map fragment by calling.FragmentManager.findFragmentById()
Then use getMapAsync()
to register for the map callback:
1 2 3 |
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map); mapFragment.getMapAsync(this); |
3. Implement the OnMapReadyCallback
interface and override the onMapReady()
method, to set up the map when the GoogleMap
object is available:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public class MapsMarkerActivity extends AppCompatActivity implements OnMapReadyCallback { // Include the OnCreate() method here too, as described above. @Override public void onMapReady(GoogleMap googleMap) { // Add a marker in Sydney, Australia, // and move the map's camera to the same location. LatLng sydney = new LatLng(-33.852, 151.211); googleMap.addMarker(new MarkerOptions().position(sydney) .title("Marker in Sydney")); googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); } } |
Now I am describing, how to URL image load with Marker. Create a CustomTarget class that implement Target Interface, and load the bitmap to Marker via BitmapDescriptorFactory. The main issue comes when we add multiple Markers and load maker image from URL because of Picasso load image take some time rapidly we add a marker in a loop so some marker is not displayed. How you fix that issue, you create multiple Target object instance and add them to the List for each Marker.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
targets.add(new Target() { @Override public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom loadedFrom) { Marker marker = mGooglemap.addMarker(new MarkerOptions() .anchor(0.0f, 1.0f) // Anchors the marker on the bottom left .icon(BitmapDescriptorFactory.fromBitmap(bitmap)) .position(latLng)); marker.setTag(position); vendorMarker.add(marker); } @Override public void onBitmapFailed(Drawable drawable) { } @Override public void onPrepareLoad(Drawable drawable) { } }); Picasso.with(this).load(vendor.getIcon()).into(targets.get(i)); |
For more About Picasso.
References: https://developers.google.com/maps/documentation/android-sdk/map-with-marker
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
2 comments
For a Single marker
Picasso.with(this).load("Your Image URL").into(new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom loadedFrom) {
Marker marker = mGooglemap.addMarker(new MarkerOptions()
.anchor(0.0f, 1.0f) // Anchors the marker on the bottom left
.icon(BitmapDescriptorFactory.fromBitmap(bitmap))
.position(latLng));
marker.setTag(position);
vendorMarker.add(marker);
}
@Override
public void onBitmapFailed(Drawable drawable) {
}
@Override
public void onPrepareLoad(Drawable drawable) {
}
}
);