如何使用OnMarkerClick为谷歌地图Android api打开一个新活动

时间:2016-09-12 08:30:26

标签: android google-maps-markers

我尝试使用startActivity,但它不起作用。

这是我的代码:

enter image description here

setOnMarkerClickListener:

enter image description here

2 个答案:

答案 0 :(得分:1)

尝试这样......

public class MarkerDemoActivity extends Activity or FragmentActivity
    implements OnMarkerClickListener
{
private Marker myMarker;    
 ..............
private void setUpMap()
{
    .......
    googleMap.setOnMarkerClickListener(this);

    myMarker = googleMap.addMarker(new MarkerOptions()
                .position(latLng)
                .title("My Spot")
                .snippet("This is my spot!")
                .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
    ......
}

@Override
public boolean onMarkerClick(final Marker marker) {

    if (marker.equals(myMarker)) 
    {
       Intent intent=new Intent(MarkerDemoActivity.this,AnotherActivity.class);
      startActivity();
    }
}
}

答案 1 :(得分:0)

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")){
                    Intent intent = new Intent(MapsActivity.this, LoginActivity.class);
                startActivity(intent);
                return false;
            }
        });

    }