简体   繁体   English

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

[英]How to open different activities depending OnInfoWindowClick

I am busy creating an application in Android Studio that uses Maps API. 我正在Android Studio中使用Maps API创建应用程序。
I have successfully created OnInfoWindowClickListener to my Google Map. 我已经成功创建了OnInfoWindowClickListener到我的Google Map。 I have 2 different markers I set on my Google Map, but I want to set thousands of markers eventually. 我在Google地图上设置了2个不同的标记,但最终要设置成千上万个标记。
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. 例如,如果用户单击“标记A”,则必须打开“活动A”。 Or if a user clicks on 'Marker B', then 'Activity B' must open. 或者,如果用户单击“标记B”,则必须打开“活动B”。 Or if a user clicks on 'Marker C', then 'Activity C' must open. 或者,如果用户单击“标记C”,则必须打开“活动C”。

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? 我试图在代码中使用“ if语句和else if语句”,但是当我单击“标记的信息窗口”时,什么都没发生?

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

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)方法的public void onInfoWindowClick(Marker marker)

do something like this: 做这样的事情:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不使用 position 的情况下打开不同的活动 - How to different open activities without using position 如何用listView项打开不同的活动? - How to open different Activities with my listView items? 我需要 PendingIntent 根据用户收到的通知类型打开不同的活动 - I need for the PendingIntent to open up different Activities depending on the type of notification that the user receives 如何在Recycleview内部实现onClick监听器打开不同的Activity - How to Implemet onClick listener insidie Recycleview to open different activities 碎片和通知:将不同的活动定位于通知; 取决于屏幕配置 - Fragments and Notifications: Target different Activities from Notification; depending on screen configuration 如何使用Android App中的Firebase Auth为某些电子邮件地址用户打开不同的活动 - How do you open different activities for certain e mail address users using Firebase Auth in Android App 如何从不同的活动弹出窗口 - How to popupWindow from different activities OnClick Listener Recyclerview 打开不同的活动打开 pdf 文件 - OnClick Listener Recyclerview to open different activities to open pdf files 如何在不同的活动中演奏不同的音乐? - How to play different musics in different activities? Android-如何判断当前正在打开哪些活动? - Android - How to tell what Activities are currently open?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM