自定义InfoWindow的ClickListener事件为所有标记返回相同的值。

时间:2018-11-22 19:33:06

标签: android

代码运行良好,没有错误,但是自定义信息窗口的click事件为两个标记返回相同的值(toast消息),我需要自定义Infowindows在单击时返回特定的Toast消息。

代码:

公共类HotelActivity扩展FragmentActivity实现OnMapReadyCallback {

private GoogleMap mMap;
private final static int MY_PERMISSION_FINE_LOCATION = 101;

public static final String Hotdetails = "com.myaddress.Hotdetails";

@Override
public Resources getResources(){
    return super.getResources();
}

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

    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){// ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED){

    LatLng location1 = new LatLng(9.0454, 7.4797);
    LatLng location2 = new LatLng(9.036190, 7.480130);

    //for location1

        Marker ClassMark = mMap.addMarker(new MarkerOptions().position(eclass).title("E-Class Resorts").snippet("3 star resort of excellence").icon(BitmapDescriptorFactory.fromResource(R.drawable.eclasses)));

        InfoWindowData Classinfo = new InfoWindowData();
        Classinfo.setImage(("eclass"));
        Classinfo.setRatings("the very best");

        CustomInfoWindowGoogleMap ClassInfoWindow = new CustomInfoWindowGoogleMap(this);
        mMap.setInfoWindowAdapter(ClassInfoWindow);
        ClassMark.setTag(Classinfo);

        mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
            @Override
            public void onInfoWindowClick(Marker marker) {

                Toast.makeText(getApplicationContext(), "This is E-class", Toast.LENGTH_LONG).show();                   
            }
        });

        //for Location2

        Marker StateMark = mMap.addMarker(new MarkerOptions().position(Statement).title("Statement HOTEL").snippet("5 star hotel of excellence").icon(BitmapDescriptorFactory.fromResource(R.drawable.statementes)));

        InfoWindowData stateinfo = new InfoWindowData();
        stateinfo.setImage(("allstates"));
        stateinfo.setRatings("quite nice");           

        CustomInfoWindowGoogleMap stateInfoWindow = new CustomInfoWindowGoogleMap(this);
        mMap.setInfoWindowAdapter(stateInfoWindow);

        StateMark.setTag(stateinfo);

        mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
            @Override
            public void onInfoWindowClick(Marker marker) {

                Toast.makeText(getApplicationContext(), "This is Statement", Toast.LENGTH_LONG).show();


            }
        });

         mMap.setMyLocationEnabled(true);
        mMap.animateCamera(CameraUpdateFactory.zoomTo(4.0f));
        //mMap.animateCamera(CameraUpdateFactory.zoomIn());
        mMap.getUiSettings().setZoomControlsEnabled(true);
        mMap.setMinZoomPreference(4.0f);
        mMap.setMaxZoomPreference(17.0f);
        //mMap.resetMinMaxZoomPreference();

    } else {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, MY_PERMISSION_FINE_LOCATION);
           }
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode) {
        case MY_PERMISSION_FINE_LOCATION:
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){ //&& ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED){

                mMap.setMyLocationEnabled(true);
                    //mMap.animateCamera(CameraUpdateFactory.zoomTo(13.0f));
                    mMap.moveCamera(CameraUpdateFactory.zoomTo(14.0f));
                    mMap.getUiSettings().setZoomControlsEnabled(true);
                    mMap.setMinZoomPreference(8.0f);
                    mMap.setMaxZoomPreference(17.0f);
                }

            } else {
                Toast.makeText(getApplicationContext(), "This service requires location permissions to be granted", Toast.LENGTH_LONG).show();

            }
            break;
            }
        }

    }

1 个答案:

答案 0 :(得分:0)

设置第二个setOnInfoWindowClickListener时,您要擦除的是前一个,因为GoogleMap对象只能有一个。

代替此方法,只设置一次侦听器,然后在此方法中,使用onInfoWindowClick(Marker marker)方法中传递的“标记”参数查找用户单击的标记。

尝试这样的事情:

mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
    @Override
    public void onInfoWindowClick(Marker marker) {
        if (marker.equals(StateMark)) {
            Toast.makeText(getApplicationContext(), "This is Statement", Toast.LENGTH_LONG).show();
        } else if (marker.equals(ClassMark)) {
            Toast.makeText(getApplicationContext(), "This is E-Class", Toast.LENGTH_LONG).show();
        }

    }
});