Search code examples
androidandroid-intentandroid-maps

startActivity illegal start of type


As of this morning I've tried to put together enough knowledge to produce a very basic app to demonstrate a concept. The idea is to display google maps, the user presses on where they want to add a marker, then a screen pops up where they can fill out more information that is then displayed when someone taps that marker.

This is what I got from the Android Studio base.

 public class MapsActivity extends FragmentActivity implements 

OnMapReadyCallback {

private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}

private void setMapLongClick(final GoogleMap map) {
    map.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
        @Override
        public void onMapLongClick(LatLng latLng) {
        }
    });
}

/**
 * Manipulates the map once available.
 * This callback is triggered when the map is ready to be used.z
 * If Google Play services is not installed on the device, the user will be prompted to install
 * it inside the SupportMapFragment. This method will only be triggered once the user has
 * installed Google Play services and returned to the app.
 */
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    // Move the camera to Delft
    LatLng delft = new LatLng(52.003569, 4.372987);
    Float zoom = 15f;
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(delft, zoom));
    setMapLongClick(mMap);
}
}

I tried adding this

    private void setMarkerClick(final GoogleMap map) {
    map.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
        @Override
        Intent intent = new Intent(this, MainActivity.class);
        this.startActivity(intent);
    });
}

But I get and "illegal start of type" error. Am I doing this completely wrong?
Is there an easier way to add information to a marker?


Solution

  • This is how you should use it.

    map.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
    
        @Override
        public boolean onMarkerClick(Marker marker) {
            Intent intent = new Intent(MainActivity.this, MainActivity.class);
            startActivity(intent);
            return true;
        }
    });