Here is my whole code of a map activity with 4 clickable markers. Click on a marker shows an info window, and after click on info window you are going to another activity: English, German, Spanish or Italian. If you want to use OnMarkerClickListener in spite of OnInfoWindowClickListener, you just have to swap this line:
mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener()
to this:
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener()
this line:
public void onInfoWindowClick(Marker arg0)
to this:
public boolean onMarkerClick(Marker arg0)
and at the end of the method "onMarkerClick":
return true;
I think it may be helpful for someone ;)
package pl.pollub.translator;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
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 MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
Toast.makeText(this, "Choose a language.", Toast.LENGTH_LONG).show();
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener()
{
@Override
public void onInfoWindowClick(Marker arg0) {
if(arg0 != null && arg0.getTitle().equals("English")){
Intent intent1 = new Intent(MapsActivity.this, English.class);
startActivity(intent1);}
if(arg0 != null && arg0.getTitle().equals("German")){
Intent intent2 = new Intent(MapsActivity.this, German.class);
startActivity(intent2);}
if(arg0 != null && arg0.getTitle().equals("Italian")){
Intent intent3 = new Intent(MapsActivity.this, Italian.class);
startActivity(intent3);}
if(arg0 != null && arg0.getTitle().equals("Spanish")){
Intent intent4 = new Intent(MapsActivity.this, Spanish.class);
startActivity(intent4);}
}
});
LatLng greatBritain = new LatLng(51.30, -0.07);
LatLng germany = new LatLng(52.3107, 13.2430);
LatLng italy = new LatLng(41.53, 12.29);
LatLng spain = new LatLng(40.25, -3.41);
mMap.addMarker(new MarkerOptions()
.position(greatBritain)
.title("English")
.snippet("Click on me:)"));
mMap.addMarker(new MarkerOptions()
.position(germany)
.title("German")
.snippet("Click on me:)"));
mMap.addMarker(new MarkerOptions()
.position(italy)
.title("Italian")
.snippet("Click on me:)"));
mMap.addMarker(new MarkerOptions()
.position(spain)
.title("Spanish")
.snippet("Click on me:)"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(greatBritain));
mMap.moveCamera(CameraUpdateFactory.newLatLng(germany));
mMap.moveCamera(CameraUpdateFactory.newLatLng(italy));
mMap.moveCamera(CameraUpdateFactory.newLatLng(spain));
}
}