截至今天早上,我已经尝试将足够的知识组合在一起,以产生一个非常基本的应用程序来演示一个概念。这个想法是显示谷歌地图,用户按下他们想要添加标记的地方,然后弹出一个屏幕,他们可以填写更多信息,然后当有人点击该标记时显示这些信息。

这是我从Android Studio基地获得的。

 public class MapsActivity extends FragmentActivity implements

OnMapReadyCallback {

private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // 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);
}

private void setMapLongClick(final GoogleMap map) {
    map.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
        @Override
        public void onMapLongClick(LatLng latLng) {
        }
    });
}

/**
 * Manipulates the map once available.
 * This callback is triggered when the map is ready to be used.z
 * If Google Play services is not installed on the device, the user will be prompted to install
 * it inside the SupportMapFragment. This method will only be triggered once the user has
 * installed Google Play services and returned to the app.
 */
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    // Move the camera to Delft
    LatLng delft = new LatLng(52.003569, 4.372987);
    Float zoom = 15f;
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(delft, zoom));
    setMapLongClick(mMap);
}
}


我尝试添加这个

    private void setMarkerClick(final GoogleMap map) {
    map.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
        @Override
        Intent intent = new Intent(this, MainActivity.class);
        this.startActivity(intent);
    });
}


但是我得到和“非法开始类型”错误。我这样做是完全错误的吗?
有没有更简单的方法将信息添加到标记?

最佳答案

这就是您应该如何使用它。

map.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {

    @Override
    public boolean onMarkerClick(Marker marker) {
        Intent intent = new Intent(MainActivity.this, MainActivity.class);
        startActivity(intent);
        return true;
    }
});

10-04 20:02