如何使谷歌Mapapi标记可点击并打开新的活动?

jchrr9hc  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(276)

我希望有我的标记,这样当单击它们时,它们将打开一个新的活动,该活动将显示来自sqlite数据库的位置列表。如果有人能给我看代码添加到我的标记打开活动的特定标记,这将是伟大的。
公共类mapsactivity扩展了fragmentactivity实现onmapreadycallback{

private GoogleMap mMap;

private static final LatLng Kerry = new LatLng(52.212636048, -9.812321255);
private static final LatLng Limerick = new LatLng(52.653131468516236, -8.632676814862014);
private static final LatLng Clare = new LatLng(52.887135246510255, -9.063511195184182);
private static final LatLng Galway = new LatLng(53.27726074573369, -9.065918590802234);
private static final LatLng Mayo = new LatLng(53.88316732870579, -9.42381429549789);
private static final LatLng Sligo = new LatLng(54.20195132022076, -8.59144563453587);
private static final LatLng Leitrim = new LatLng(54.340224636620334, -8.037324331196);
private static final LatLng Donegal = new LatLng(55.013236950319495, -8.226710776524827);
private static final LatLng Cork = new LatLng(51.85961876429461, -8.49543099484229);

private Marker mKerry;
private Marker mLimerick;
private Marker mClare;
private Marker mGalway;
private Marker mMayo;
private Marker mSligo;
private Marker mLeitrim;
private Marker mDonegal;
private Marker mCork;

private LocationManager locationManager;
private LocationListener locationListener;

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

/**
 * Manipulates the map once available.
 * This callback is triggered when the map is ready to be used.
 * This is where we can add markers or lines, add listeners or move the camera. In this case,
 * we just add a marker near Sydney, Australia.
 * If Google Play services is not installed on the device, the user will be prompted to install
 * it inside the SupportMapFragment. This method will only be triggered once the user has
 * installed Google Play services and returned to the app.
 */
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    locationListener = new LocationListener() {
        @Override
        public void onLocationChanged(@NonNull Location location) {
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }

        @Override
        public void onProviderEnabled(String provider) {
        }

        @Override
        public void onProviderDisabled(String provider) {

        }
    };

    ///array list for markers
    List<Marker> markerList = new ArrayList<>();

    googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    mKerry = googleMap.addMarker(new MarkerOptions()
            .position(Kerry)
            .title("Kerry")
            .snippet("Confirmed cases past 14 days: 44"));
    mKerry.setTag(0);
    markerList.add(mKerry);

    mCork = googleMap.addMarker(new MarkerOptions()
            .position(Cork)
            .title("Cork")
            .snippet("Confirmed cases past 14 days: 191")
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW)));
    mCork.setTag(0);
    markerList.add(mCork);

    mLimerick = googleMap.addMarker(new MarkerOptions()
            .position(Limerick)
            .title("Limerick")
            .snippet("Confirmed cases past 14 days: 270")

            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW)));
    mLimerick.setTag(0);
    markerList.add(mLimerick);

    mClare = googleMap.addMarker(new MarkerOptions()
            .position(Clare)
            .title("Clare")

            .snippet("Confirmed cases past 14 days: 39")
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW)));

    mClare.setTag(0);
    markerList.add(mClare);

    mGalway = googleMap.addMarker(new MarkerOptions()
            .position(Galway)
            .title("Galway")
            .snippet("Confirmed cases past 14 days: 143")
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW)));
    mGalway.setTag(0);
    markerList.add(mGalway);

    mMayo = googleMap.addMarker(new MarkerOptions()
            .position(Mayo)
            .title("Mayo")
            .snippet("Confirmed cases past 14 days: 98")
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW)));
    mMayo.setTag(0);
    markerList.add(mMayo);

    mSligo = googleMap.addMarker(new MarkerOptions()
            .position(Sligo)
            .title("Sligo")
            .snippet("Confirmed cases past 14 days: 20")

            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW)));
    mSligo.setTag(0);
    markerList.add(mSligo);

    mLeitrim = googleMap.addMarker(new MarkerOptions()
            .position(Leitrim)
            .title("Leitrim")
            .snippet("Confirmed cases past 14 days: 5")
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW)));
    mLeitrim.setTag(0);
    markerList.add(mLeitrim);

    mDonegal = googleMap.addMarker(new MarkerOptions()
            .position(Donegal)
            .title("Donegal")
            .snippet("Confirmed cases past 14 days: 351")
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW)));
    mDonegal.setTag(0);
    markerList.add(mDonegal);

    for (Marker m : markerList) {
        LatLng latLng = new LatLng(m.getPosition().latitude, m.getPosition().longitude);
        mMap.addMarker(new MarkerOptions().position(latLng));
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 7));
        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 7));

    }

}

}
'''

wgeznvg7

wgeznvg71#

我觉得是这样的:

mMap.setOnInfoWindowClickListener(marker -> {
    Intent intent = new Intent(this, SomeActivity.class);
    intent.putExtra("Key", marker.getTag().toString());
    startActivity(intent);
});

在someactivity的某处,要检索标记的字符串标记:

String value = getIntent().getParcelableExtra("Key");

相关问题