如何使用带有标记数组的onInfoWindowClick?

时间:2013-12-07 19:51:40

标签: android google-maps-markers google-maps-android-api-2

我有一张带有很多标记的地图。单击标记时,将显示包含信息的小窗口。当你点击它我想调用片段。我发现我应该使用onInfoWindowClick但是出了点问题。我无法获得任何价值。

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();
    }
}

我发现了一些tutorials,但我不知道我做错了什么

1 个答案:

答案 0 :(得分:2)

initializeMap()函数中,使用getMap()后立即将以下代码放在那里。使用此方法,您无需调用implements OnInfoWindowClickListener,但无论如何都可以使用相同的方法。

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.
        }
    });
}

以上代码允许您在用户点击信息窗口时执行某些操作。

要首先显示信息窗口,请使用以下任一代码段之一:

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"));

或者,

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

来源:Google Maps Android API v2