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?
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 (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.
do something like this:
if (marker.getTitle().equals("Durban")) {
Intent intent = new Intent(MainActivity.this, Durban.class);
startActivity(intent);
}