onMarkerClick 永远不会被调用?

onMarkerClick is never called?

将 Google 地图添加到项目中,我刚刚创建了一个练习项目来首先对其进行测试。我希望能够单击我添加的标记,但是我收到一条错误消息,指出从未使用过 onMarkerClick 方法。

我的代码是:

package uk.co.ryanmoss.cameraproject;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ViewSwitcher;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;


public class MainActivity extends Activity {
    private ViewSwitcher switcher;
    private static final int REFRESH_SCREEN = 1;
    private GoogleMap gMap;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        switcher = (ViewSwitcher) findViewById(R.id.profileSwitcher);


        Button button1 = (Button) findViewById(R.id.button1);

        initialiseMap();


    }

    public void initialiseMap(){
        if (gMap == null){
            gMap = ((MapFragment) getFragmentManager().findFragmentById(
                    R.id.map)).getMap();
            gMap.setOnMarkerClickListener((GoogleMap.OnMarkerClickListener) this);
        }
    }

    public boolean onMarkerClick(Marker marker){
        switcher.showNext();
        return false;
    }
    public void changeScreen(View view){

        switcher.showNext();

    }

    public void changeScreenBack(View view){
        switcher.showPrevious();
        markerMap(gMap);

    }

    public void markerMap(GoogleMap map){
       map.addMarker(new MarkerOptions()
                            .position(new LatLng(53.7832138, -2.6556234))
                            .title("Hello world"))
                            .setIcon(BitmapDescriptorFactory.fromResource(R.drawable.arrowdown));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

}

您的 class 不是您在此参数中指出的 GoogleMap.OnMarkerClickListener 类型:

gMap.setOnMarkerClickListener((GoogleMap.OnMarkerClickListener) this);

方法 public boolean onMarkerClick(Marker marker)GoogleMap.OnMarkerClickListener 接口的方法,但您的 class 是 Activity。您需要创建一个包含此方法的匿名 class。

正如拜伦所说,你必须这样做:

gMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
        @Override
        public boolean onMarkerClick(Marker marker) {


            return false;
        }
});

我不知道是什么问题,但是如果你在 GoogleMap.OnMarkerClickListener 的 class 上实现接口方法不起作用,但在对象内部工作正常! 就像@kelvincer 之前说的