在 Google Maps API v3 中长按添加标记

Jol*_*lan 0 android google-maps-api-3 onlongclicklistener

如何在 Android 版 Google Maps API v3 中实现在长按时添加标记?Stack Overflow 本身有关于此的答案,但它们适用于 Google Maps API v2。

public class advertiserMap extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_advertiser_map);
    // 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);
}

//for searching for a location
public void onMapSearch(View view) {
    EditText locationSearch = (EditText) findViewById(R.id.editText);
    String location = locationSearch.getText().toString();
    List<Address> addressList = null;

    if (location != null || !location.equals("")) {
        Geocoder geocoder = new Geocoder(this);
        try {
            addressList = geocoder.getFromLocationName(location, 1);

        } catch (IOException e) {
            e.printStackTrace();
        }
        Address address = addressList.get(0);
        LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());
        mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
    }
}

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

    // Add a marker in Sydney and move the camera
    LatLng sydney = new LatLng(-34, 151);
    mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
Run Code Online (Sandbox Code Playgroud)

}

Cau*_*ini 5

正如@tyczj 指出的那样,Android Google Maps API 没有 v3,因此您可能使用的是 v2。

也就是说,要完成您想要的操作,请在您的 mMap 对象中调用 setOnMapLongClickListener,并根据需要在 onMapLongClick 中添加标记。您应该在 onMapReady 方法中执行此操作:

@Override
public void onMapReady(GoogleMap googleMap) {

    ... 

    mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
        @Override
        public void onMapLongClick(LatLng latLng) {
            googleMap.addMarker(new MarkerOptions()
                    .position(latLng)
                    .title("Your marker title")
                    .snippet("Your marker snippet"));
        }
    }); 
}
Run Code Online (Sandbox Code Playgroud)

// 编辑:

如果你想一次只保留一个标记,你应该在全局活动的范围内声明你的标记,然后,在 onMapLongClick 中,如果标记已经存在,而不是创建一个新的标记,只需更新它的位置:

public class advertiserMap extends FragmentActivity implements OnMapReadyCallback {

    // Declare marker globally
    Marker myMarker;

    ...

    @Override
    public void onMapReady(GoogleMap googleMap) {

        ... 

        mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
            @Override
            public void onMapLongClick(LatLng latLng) {

                // First check if myMarker is null
                if (myMarker == null) {

                    // Marker was not set yet. Add marker:
                    myMarker = googleMap.addMarker(new MarkerOptions()
                        .position(latLng)
                        .title("Your marker title")
                        .snippet("Your marker snippet"));

                } else {

                    // Marker already exists, just update it's position
                    myMarker.setPosition(latLng);

                }
            }
        }); 
    }
}
Run Code Online (Sandbox Code Playgroud)

如果这样做,请记住在您的代码中操作之前始终检查您的标记是否为 NULL。