Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onMarkerClick not working (v2)

I'm having some troubles with onMarkerClick

Basically for now I just want that when ANY marker previously created starts the same activity when clicked. I will implement the filtering of the marker later on.

What I get now is no error. Simply nothing happens when the marker is clicked.

Here is my source:

I'm looping through a database to populate the map and then the markers are displayed

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMarkerClickListener;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
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 NevianoMaps extends Activity implements OnMarkerClickListener{

    private GoogleMap googleMap;
    DatabaseHandler db = new DatabaseHandler(this);
    double latitude;
    double longitude;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.neviano_maps);

        try {
            // Loading map
            initilizeMap();

        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    /**
     * function to load map. If map is not created it will create it for you
     * */
    private void initilizeMap() {
        if (googleMap == null) {
            googleMap = ((MapFragment) getFragmentManager().findFragmentById(
                    R.id.map)).getMap();

            CameraPosition cameraPosition = new CameraPosition.Builder().target(
                    new LatLng(double value, double value)).zoom(12).build();

            googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

            //Luoghi di interesse check
            if (getIntent().getIntExtra("str1", 0) == 0){
                for(int x = 1; x < 6; x = x+1) {
                    latitude = db.getCultura(x).getCoordLat();
                    longitude = db.getCultura(x).getCoordLong();

                    googleMap.addMarker(new MarkerOptions()
                    .position(new LatLng(latitude, longitude)).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
                    .title(db.getCultura(x).getName()));
                }
            }
            if (getIntent().getIntExtra("str1", 1) == 1){
                googleMap.addMarker(new MarkerOptions()
                .position(new LatLng(double value, double value)).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN))
                .title("Sport"));
            }

            // check if map is created successfully or not
            if (googleMap == null) {
                Toast.makeText(getApplicationContext(),
                        "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                        .show();
            }
        }
    }

    @Override
    public boolean onMarkerClick(Marker arg0) {
        Intent i = new Intent(this, informazioni.class);
        //i.putExtra("str1", db.getCulturaName(arg0.getTitle()).getDescription());
        startActivity(i);
        return false;
    }

    @Override
    protected void onResume() {
        super.onResume();



        initilizeMap();
    }

}

Hope somebody can help! Thank you

like image 758
Francesco Avatar asked Oct 08 '13 19:10

Francesco


1 Answers

In your initilizeMap() method, please add this line after you've got your map:

googleMap.setOnMarkerClickListener(this);
like image 153
fasteque Avatar answered Oct 31 '22 19:10

fasteque