Serafins
Serafins

Reputation: 1247

How to use onInfoWindowClick with array of markers?

I have a map with a lot of markers. When you click marker, small windows with information will appear. When you click it I want to call fragment. I found that I should use onInfoWindowClick but something is wrong. I can't get any values.

public class Map extends Activity implements OnInfoWindowClickListener{

    static final LatLng xxx = new LatLng(70.000, 70,22);
    static String[] streets;
    static String[] artist;
    Coordinate cor = new Coordinate();

    private GoogleMap googleMap;

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

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

        Resources res = this.getResources();
        artist = res.getStringArray(R.array.authors_nicks);
        streets = res.getStringArray(R.array.streets);

        for (int i = 0; i < cor.coordinatesVale.size(); i++) {
            Marker m = googleMap.addMarker(new MarkerOptions()
                    .position(cor.coordinatesVale.get(i))
                    .title(artist[i])
                    .snippet(streets[i])
                    .icon(BitmapDescriptorFactory
                            .fromResource(R.drawable.marker)));
                    m.getId();
        }

        googleMap.setMyLocationEnabled(true);

        googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(xxx, 12));
        googleMap.animateCamera(CameraUpdateFactory.zoomTo(12), 2000, null);

    }


@Override
        public void onInfoWindowClick(Marker marker) {
         String id = marker.getId();
         Intent i = new Intent(getApplicationContext(), MainActivity.class);
         i.putExtra("dsdsd", 3);
         startActivity(i);
         Log.i("dddd", id);  /// CAN't see
     }

    private void initilizeMap() {
        if (googleMap == null) {
            googleMap = ((MapFragment) getFragmentManager().findFragmentById(
                    R.id.map)).getMap();

            if (googleMap == null) {
                Toast.makeText(getApplicationContext(),
                        "Unable to create maps", Toast.LENGTH_SHORT)
                        .show();
            }
        }
    }

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

I found some tutorials, but I'm don't know what I'm doing wrong

Upvotes: 0

Views: 3519

Answers (1)

Anonsage
Anonsage

Reputation: 8320

In your initializeMap() function, right after you use getMap(), put the following code there. Using this method, you don't need to call implements OnInfoWindowClickListener, but would work the same either way.

if (googleMap != null) {

    // More info: https://developers.google.com/maps/documentation/android/infowindows
    mMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
        @Override
        public void onInfoWindowClick(Marker marker) {
            // Determine what marker is clicked by using the argument passed in
            // for example, marker.getTitle() or marker.getSnippet().
            // Code here for navigating to fragment activity.
        }
    });
}

The above code allows you to do something when the user clicks on the info window.

In order to show the info window in the first place, use one either one of the following code snippets, which you have already:

static final LatLng MELBOURNE = new LatLng(-37.81319, 144.96298);
Marker melbourne = mMap.addMarker(new MarkerOptions()
        .position(MELBOURNE)
        .title("Melbourne")
        .snippet("Population: 4,137,400"));

Or,

Marker melbourne = mMap.addMarker(new MarkerOptions()
        .position(MELBOURNE)
        .title("Melbourne"));
melbourne.showInfoWindow();

Source: Google Maps Android API v2

Upvotes: 2

Related Questions