[英]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.