[英]How to open different activities depending OnInfoWindowClick
我正在Android Studio中使用Maps API创建应用程序。
我已经成功创建了OnInfoWindowClickListener到我的Google Map。 我在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();
}
}
}
您正在if子句中进行如下检查:
if (googleMap.equals("Durban")) {
Intent intent = new Intent(MainActivity.this, Durban.class);
startActivity(intent);
}
但是您应该检查public void onInfoWindowClick(Marker marker)
方法的public void onInfoWindowClick(Marker marker)
。
做这样的事情:
if (marker.getTitle().equals("Durban")) {
Intent intent = new Intent(MainActivity.this, Durban.class);
startActivity(intent);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.