简体   繁体   English

在 Android 中向 Google 地图添加自定义标记

[英]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.我有一个 Android 应用程序,使用 java 和谷歌地图活动制作,我想用一个新的标记替换“默认 sidney”市场,用我通过参数给出的 LatLng,但我不知道如何。 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.但是无论如何,“ubicacion”变量都是在 null 上设置的。 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.我不想更改标记的样式,我想通过参数为 onMapReady 方法提供 LatLng,而不是像所有示例所示的那样直接在 class 上设置它。

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))只需添加.icon(BitmapDescriptorFactory.fromResource(R.drawable.my_icon))

or changing colors of default marker .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))或更改默认标记的.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您必须在 onMapReady 中调用 setLatLng 方法

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));
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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