谷歌地图Android API v2检测长按地图并添加标记不起作用

时间:2013-04-19 04:19:16

标签: android google-maps-markers latitude-longitude google-maps-android-api-2 supportmapfragment

我想在长按时在地图上添加标记。

Toast中的{p> onMapClick()显示正常点按。但长按不起作用。长按不显示Toast中的onMapLongClick()。地图上也不显示标记。

我正在使用SupportMapFragment因为我想在Android 2.x设备上使用我的应用程序。我已经在Nexus One上测试了我的应用程序,它的Android版本为2.3.7。

这是我的代码。

public class MainActivity extends FragmentActivity implements
        OnMapClickListener, OnMapLongClickListener {

    final int RQS_GooglePlayServices = 1;
    private GoogleMap myMap;

    Location myLocation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FragmentManager myFragmentManager = getSupportFragmentManager();
        SupportMapFragment mySupportMapFragment = (SupportMapFragment) myFragmentManager
                .findFragmentById(R.id.map);
        myMap = mySupportMapFragment.getMap();

        myMap.setMyLocationEnabled(true);

        myMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
        // myMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        // myMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
        // myMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);

        myMap.setOnMapClickListener(this);
        myMap.setOnMapLongClickListener(this);

    }

    @Override
    protected void onResume() {
        super.onResume();

        int resultCode = GooglePlayServicesUtil
                .isGooglePlayServicesAvailable(getApplicationContext());

        if (resultCode == ConnectionResult.SUCCESS) {
            Toast.makeText(getApplicationContext(),
                    "isGooglePlayServicesAvailable SUCCESS", Toast.LENGTH_LONG)
                    .show();
        } else {
            GooglePlayServicesUtil.getErrorDialog(resultCode, this,
                    RQS_GooglePlayServices);
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onMapClick(LatLng point) {

        myMap.animateCamera(CameraUpdateFactory.newLatLng(point));

        Toast.makeText(getApplicationContext(), point.toString(),
                Toast.LENGTH_LONG).show();


    }

    @Override
    public void onMapLongClick(LatLng point) {

        myMap.addMarker(new MarkerOptions().position(point).title(
                point.toString()));

        Toast.makeText(getApplicationContext(),
                "New marker added@" + point.toString(), Toast.LENGTH_LONG)
                .show();

    }

}

我该如何解决这个问题?

3 个答案:

答案 0 :(得分:39)

我用过这个。它奏效了。

GoogleMap gm;

gm.setOnMapLongClickListener(this); 

@Override    
public void onMapLongClick(LatLng point) {
    gm.addMarker(new MarkerOptions()
        .position(point)
        .title("You are here")           
        .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));  
}

答案 1 :(得分:1)

您没有设置图标。 尝试:

    @Override
    public void onMapLongClick(LatLng point) {

        myMap.addMarker(new MarkerOptions()
            .position(point)
            .title(point.toString())
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED))); 

         Toast.makeText(getApplicationContext(),
            "New marker added@" + point.toString(), Toast.LENGTH_LONG)
            .show();

} 

答案 2 :(得分:1)

googleMap = supportMapFragment.getMap();

    // Setting a click event handler for the map
    googleMap.setOnMapClickListener(new OnMapClickListener() {

        @Override
        public void onMapClick(LatLng latLng) {

            // Creating a marker
            MarkerOptions markerOptions = new MarkerOptions();

            // Setting the position for the marker
            markerOptions.position(latLng);

            // Setting the title for the marker.
            // This will be displayed on taping the marker
            markerOptions.title(latLng.latitude + " : " + latLng.longitude);

            // Clears the previously touched position
            googleMap.clear();

            // Animating to the touched position
            googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));

            // Placing a marker on the touched position
            googleMap.addMarker(markerOptions);
        }
    });

mMap.setOnMapLongClickListener(new
           GoogleMap.OnMapLongClickListener() {
        @Override
        public void onMapLongClick (LatLng latLng){
           Geocoder geocoder =
              new Geocoder(MapMarkerActivity.this);
           List<Address> list;
           try {
              list = geocoder.getFromLocation(latLng.latitude,
                 latLng.longitude, 1);
           } catch (IOException e) {
              return;
           }
           Address address = list.get(0);
           if (marker != null) {
              marker.remove();
           }

           MarkerOptions options = new MarkerOptions()
              .title(address.getLocality())
              .position(new LatLng(latLng.latitude,
                 latLng.longitude));

           marker = mMap.addMarker(options);
        }
     });