如何使用双击标记为Android谷歌地图应用程序打开一项新活动?

时间:2016-09-17 05:50:03

标签: android google-maps google-maps-markers

我尝试使用onMarkerClick,它允许用户在点击标记时查看新的活动/窗口。如果想要更改行为以便打开新活动,但只有当我双击标记时,我该怎么做?

这是我的代码:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, OnMarkerClickListener {

private Marker myMarker;

ChildEventListener mChildEventListner;
DatabaseReference mProfileRef = FirebaseDatabase.getInstance().getReference("Profile");


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

}


@Override
public void onMapReady(GoogleMap googleMap) {

    googleMap.setOnMarkerClickListener(this);

    LatLng  wollongong = new LatLng(-34.506081, 150.88104);
    googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(wollongong, 18));
    googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);

    addMarkersToMap(googleMap);

    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
    googleMap.setMyLocationEnabled(true);
}

@Override
public  void onStop(){
    if(mChildEventListner != null)
        mProfileRef.removeEventListener(mChildEventListner);
    super.onStop();
}

public void addMarkersToMap(final GoogleMap map){
    mChildEventListner = mProfileRef.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            FirebaseMarker marker = dataSnapshot.getValue(FirebaseMarker.class);
            String dob = marker.getDob();
            String dod = marker.getDod();
            double latitude = marker.getLatitude();
            double longitude = marker.getLongitude();
            String firstname = marker.getFirstname();
            String lastname = marker.getLastname();

            LatLng location = new LatLng(latitude,longitude);
            map.addMarker(new MarkerOptions().position(location).title(firstname +" "+ lastname).snippet(dob +" - "+ dod).icon(BitmapDescriptorFactory.fromResource(R.drawable.marker)));

        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}



@Override
public boolean onMarkerClick(Marker marker) {

    if (marker.equals(myMarker))
    {
        Intent intent = new Intent(MapsActivity.this,Info_window.class);
        startActivity(intent);
    }
    else
    {
        Intent intent = new Intent(MapsActivity.this,Info_window.class);
        startActivity(intent);
    }
    return false;
}

任何人都可以帮助我吗?非常感谢!

1 个答案:

答案 0 :(得分:5)

定义以下"全球"变量:

boolean doubleBackToExitPressedOnce = false;

然后在onMarkerClick内修改代码,如下所示:

if (doubleBackToExitPressedOnce) {
    Intent intent = new Intent(MapsActivity.this, Info_window.class);
    startActivity(intent);

} else {
    this.doubleBackToExitPressedOnce = true;

    new Handler().postDelayed(new Runnable() {    
        @Override
        public void run() {
            doubleBackToExitPressedOnce = false;
        }
    }, 2000);
}

不要忘记在导入Handler之前:

import android.os.Handler;