I am trying to create an Android application in Android Studio, that works with the Google Maps API. I have successfully added the marker to the map to a certain LatLng position I have chosen.
When the marker is clicked, it brings up a title called "Testing". But what I want, is when a user clicks on the markers title, it must open a new activity. However, I can't seem to get it to work. I have added a onMarkerClick and however, I can't implement it. I am really confused. I have tried to add a callback method but not sure how to.
And would you just mind showing me the correct code? Thank you, much appreciated in advance!
package com.msp.googlemapsproject;
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 android.support.v4.app.FragmentActivity
implements GoogleMap.OnMarkerClickListener {
static final LatLng MyHome = new LatLng(-29.759933, 30.801030);
private GoogleMap googleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
googleMap.setOnMarkerClickListener(this);
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 MyHome_display = googleMap.addMarker(new MarkerOptions().position(MyHome).title("Testing"));
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public boolean onMarkerClick(Marker marker) {
if (marker.equals(MyHome))
{
Intent intent = new Intent(MainActivity.this, LastScreen.class);
startActivity(intent);
}
return false;
}
}