0

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

1
  • Probably I should implement markers not in onCreate. But I don;t know where should I do it Commented Dec 7, 2013 at 20:05

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

@Serafins, What exactly are you trying to do? Aka, what will the Fragment be used for? There are two different things you can depending on what you are trying to accomplish. If the Fragment is supposed to replace your entire Map Activity, then you need to call an Activity that in turn shows the Fragment. Or, you can add a Fragment to your current Map Activity by programmatically adding a View that shows the Fragment.
question from marker window I want to call fragment with this id

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.