我无法检测到我的手机或模拟器上的长按

时间:2018-07-17 14:05:02

标签: android google-maps

长时间单击手机或模拟器时,我无法检测到任何东西。另外,我没有收到任何错误或Logcat消息。我目前正在使用Android Studio 3.1.3。

package com.example.gaurangadas.test;

import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback,GoogleMap.OnMapLongClickListener {

    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // 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 = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }


    @Override
    public void onMapLongClick(LatLng latLng) {
        Toast.makeText(this, "HELLO", Toast.LENGTH_SHORT).show();
        Log.i("LONG CLICK","DETECTED");
    }
}

1 个答案:

答案 0 :(得分:1)

mMap.setOnMapLongClickListener(this);

将其放在onMapReady()之后     mMap = googleMap; 这会将您的onMapLongClick注册到mMap谷歌地图。