Android Google Map v2 - Starting activity when clicking on marker infoWindow

拜拜、爱过 提交于 2019-12-04 15:59:20

问题


I have seen that people are finding a lot of difficulties with this specific task and I am in need of help as well.

I have successfully created Markers on a Map using the default code that Google has provided. But now I want to be able to click on "InfoWindow" to open a new activity so I can add more information.

  • From this basic marker with Title and Snippet:

    http://mobisys.in/blog/wp-content/uploads/2013/04/Screenshot_2013-04-04-17-19-581.png

  • Then to click on it and open a blank activity:

    http://tuts-authors.s3.amazonaws.com/mobile.tutsplus.com/Shane%20Conder%20and%20Lauren%20Darcey/2012/09/25/Android-Creating-Hello-Worlds_Emulator-basic-hello-world.png

Does anyone know the best way to do this?

If you can answer this please put up some code or an example. Any help would be much appreciated!


回答1:


add this to your code

 Mymap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
            @Override
            public void onInfoWindowClick(Marker marker) {
               Intent intent = new Intent(MapActivity.this,OtherActivity.class);
               startActivity(intent);


            }
        });



回答2:


This method works even well with multiple markers. get the title of the marker using marker.getTitle() and Starts the activity based on which marker you clicked. 

public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        // Add a marker in Sydney and move the camera
        LatLng chennai = new LatLng(12.9671, 80.2593);
        mMap.addMarker(new MarkerOptions().position(chennai).title("Chennai"));

        LatLng perungudi = new LatLng(12.97, 80.25);
        mMap.addMarker(new MarkerOptions().position(perungudi).title("Perungudi"));

        LatLng pallikarnai = new LatLng(12.9377, 80.2154);
        mMap.addMarker(new MarkerOptions().position(pallikarnai).title("Pallikarnai"));

        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(chennai,12));
        mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker marker) {
                if (marker.getTitle().equals("Chennai")){
                    Toast.makeText(MapsActivity.this, "Clicked"+marker.getTitle(), Toast.LENGTH_SHORT).show();
                }
                return false;
            }
        });

    }


来源:https://stackoverflow.com/questions/16677929/android-google-map-v2-starting-activity-when-clicking-on-marker-infowindow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!