Reputation: 3
I would like to know how to add a custom marker to my map in android. The custom marker is in my drawable folder and is named car.png . Given below is the code for google map API v2 in android.At present
I am getting the standard google map marker
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class Map extends FragmentActivity {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mapp);
Intent intent = getIntent();
Bundle extras = intent.getExtras();
String lats = extras.getString("LATITUDE");
String longts = extras.getString("LONGITUDE");
String speeds = extras.getString("SPEED");
double latt= Double.parseDouble(lats);
double longt=Double.parseDouble(longts);
final LatLng Location=new LatLng(latt,longt);
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map1))
.getMap();
mMap.addMarker(new MarkerOptions().position(Location).title("The Speed Of The Vehicle Is: " + speeds + "kmph"));
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
CameraUpdate cam=CameraUpdateFactory.newLatLngZoom(Location,15);
mMap.animateCamera(cam);
}
}
Upvotes: 0
Views: 75
Reputation: 5420
This page has information on how to use a custom image as the marker: https://developers.google.com/maps/documentation/android/marker#customize_a_marker
Example from the above source:
private static final LatLng MELBOURNE = new LatLng(-37.813, 144.962);
private Marker melbourne = mMap.addMarker(new MarkerOptions()
.position(MELBOURNE)
.title("Melbourne")
.snippet("Population: 4,137,400")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.arrow)));
You can use fromResource
, fromFile
, fromBitmap
, and fromAsset
with the BitmapDescriptorFactory
class: https://developer.android.com/reference/com/google/android/gms/maps/model/BitmapDescriptorFactory.html
Upvotes: 3