如何根据OnInfoWindowClick打开不同的活动

时间:2022-01-27 00:28:14

I am busy creating an application in Android Studio that uses Maps API.
I have successfully created OnInfoWindowClickListener to my Google Map. I have 2 different markers I set on my Google Map, but I want to set thousands of markers eventually.
When a user clicks on the Info Window, it must open a new activity.

So example, if a user clicks on 'Marker A', then 'Activity A' must open. Or if a user clicks on 'Marker B', then 'Activity B' must open. Or if a user clicks on 'Marker C', then 'Activity C' must open.

I have attempted to use the 'if statement & else if statement' in my code, but when I click on the Marker's Info Window, nothing happens?

我正忙着在Android Studio中创建一个使用Maps API的应用程序。我已成功为我的Google Map创建了OnInfoWindowClickListener。我在Google地图上设置了2个不同的标记,但我想最终设置数千个标记。当用户单击“信息窗口”时,它必须打开一个新活动。例如,如果用户点击“标记A”,则必须打开“活动A”。或者,如果用户点击“标记B”,则必须打开“活动B”。或者,如果用户点击“标记C”,则必须打开“活动C”。我试图在我的代码中使用'if statement&else if语句',但是当我点击Marker的Info Window时,没有任何反应?

package com.msp.googlemapsproject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

public class MainActivity extends Activity {

    static final LatLng CapeTown = new LatLng(-29.759933, 30.801030);
    static final LatLng Durban = new LatLng(-29.858680, 31.021840);
    private GoogleMap googleMap;

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

        try{

            if (googleMap == null) {

                googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

            }

            googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
            googleMap.setMyLocationEnabled(true);
            googleMap.setTrafficEnabled(true);
            googleMap.setIndoorEnabled(true);
            googleMap.setBuildingsEnabled(true);
            googleMap.getUiSettings().setZoomControlsEnabled(true);

            final Marker marker_CapeTown = googleMap.addMarker(new MarkerOptions().position(CapeTown).title("Cape Town"));
            final Marker marker_Durban = googleMap.addMarker(new MarkerOptions().position(Durban).title("Durban"));

            googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {

                @Override
                public void onInfoWindowClick(Marker marker) {
                    if (googleMap.equals("Durban")) {
                        Intent intent = new Intent(MainActivity.this, Durban.class);
                        startActivity(intent);
                    }

                    else if (googleMap.equals("Cape Town")) {
                        Intent intent = new Intent(MainActivity.this, CapeTown.class);
                        startActivity(intent);
                    }
                }
            });

        } catch (Exception e) {

            e.printStackTrace();
        }
    }
}

1 个解决方案

#1


2  

you are checking like this in your if clause:

你在if子句中这样检查:

if (googleMap.equals("Durban")) {
      Intent intent = new Intent(MainActivity.this, Durban.class);
      startActivity(intent);
}

but you should check on the marker of the public void onInfoWindowClick(Marker marker) method.

但你应该检查public void onInfoWindowClick(Marker marker)方法的标记。

do something like this:

做这样的事情:

if (marker.getTitle().equals("Durban")) {
      Intent intent = new Intent(MainActivity.this, Durban.class);
      startActivity(intent);
}

#1


2  

you are checking like this in your if clause:

你在if子句中这样检查:

if (googleMap.equals("Durban")) {
      Intent intent = new Intent(MainActivity.this, Durban.class);
      startActivity(intent);
}

but you should check on the marker of the public void onInfoWindowClick(Marker marker) method.

但你应该检查public void onInfoWindowClick(Marker marker)方法的标记。

do something like this:

做这样的事情:

if (marker.getTitle().equals("Durban")) {
      Intent intent = new Intent(MainActivity.this, Durban.class);
      startActivity(intent);
}