gpt4 book ai didi

android - 如何使用 Android Google map 应用程序的双击标记打开新 Activity ?

转载 作者:行者123 更新时间:2023-12-02 16:09:10 26 4
gpt4 key购买 nike

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

这是我的代码:

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

有人可以帮我吗?非常感谢!

最佳答案

定义以下“全局”变量:

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;

关于android - 如何使用 Android Google map 应用程序的双击标记打开新 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39543304/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com