【问题标题】:How to open different activities depending OnInfoWindowClick如何根据 OnInfoWindowClick 打开不同的活动
【发布时间】:2017-08-01 15:15:56
【问题描述】:

我正忙着在 Android Studio 中创建一个使用 Maps API 的应用程序。
我已成功为我的 Google 地图创建了 OnInfoWindowClickListener。我在 Google 地图上设置了 2 个不同的标记,但最终我想设置数千个标记。
当用户单击信息窗口时,它必须打开一个新活动。

例如,如果用户单击“标记 A”,则必须打开“活动 A”。或者,如果用户单击“标记 B”,则必须打开“活动 B”。或者,如果用户单击“标记 C”,则必须打开“活动 C”。

我曾尝试在我的代码中使用“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();
        }
    }
}

【问题讨论】:

    标签: java android google-maps google-maps-api-3 infowindow


    【解决方案1】:

    你在 if 子句中这样检查:

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

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

    做这样的事情:

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

    【讨论】:

    • 非常感谢。在过去的几周里,我一直在网上寻找答案,不胜感激!代码有效!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-07
    • 1970-01-01
    • 1970-01-01
    • 2019-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多