简体   繁体   中英

Add Custom Marker to Google Maps in Android

I have an Android App made with java with a Google Maps activity and I wanted to replace the "default sidney" market with a new marker with the LatLng that i am giving in by parameter, but i dont know how. I`ve tried this =>

public class MapsActivity2 extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;
    private double latitud;
    private double longitud;
private LatLng ubicacion;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps2);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }


    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        // Add a marker in Sydney and move the camera
        LatLng sydney = ubicacion;
        mMap.addMarker(new MarkerOptions().position(sydney).title("Ubicacion Gasto"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }

    public void setearUbicacion(double lat, double longi){
         ubicacion = new LatLng(lat, longi);
    }
}

But the "ubicacion" variable is setted on null anyways. I don´t want to change the Style of the marker, i want to give the onMapReady method the LatLng by parameter instead of setting it up on the class directly like all the examples show.

Thanks in advance!

Market? or Marker? if Marker you can try this code

map.addMarker(new MarkerOptions()
   .position(new LatLng(latitude, longitude))                          
   .title("Title")
   .snippet("Snippet")
   .icon(BitmapDescriptorFactory.fromResource(R.drawable.my_icon)));

just add .icon(BitmapDescriptorFactory.fromResource(R.drawable.my_icon))

or changing colors of default marker .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))

public void onMapReady(GoogleMap googleMap) {
            mMap = googleMap;

            setLatLng(1.0,1.0);
   }

Try this. You have to call setLatLng method in onMapReady

public void setLatLng(double latitude , double longitude){
        LatLng location = new LatLng(latitude, longitude);
        mMap.addMarker(new MarkerOptions().position(location).title("Marker in Your Location"));
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(location,15));
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM