我在onMarkerClick上遇到了一些麻烦

基本上现在,我只希望先前创建的ANY标记在单击时启动相同的 Activity 。稍后我将实现标记的过滤。

我现在得到的是没有错误。单击标记时,什么也不会发生。

这是我的资料来源:

我正在遍历数据库以填充 map ,然后显示标记

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMarkerClickListener;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
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 NevianoMaps extends Activity implements OnMarkerClickListener{

    private GoogleMap googleMap;
    DatabaseHandler db = new DatabaseHandler(this);
    double latitude;
    double longitude;


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

        try {
            // Loading map
            initilizeMap();

        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    /**
     * function to load map. If map is not created it will create it for you
     * */
    private void initilizeMap() {
        if (googleMap == null) {
            googleMap = ((MapFragment) getFragmentManager().findFragmentById(
                    R.id.map)).getMap();

            CameraPosition cameraPosition = new CameraPosition.Builder().target(
                    new LatLng(double value, double value)).zoom(12).build();

            googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

            //Luoghi di interesse check
            if (getIntent().getIntExtra("str1", 0) == 0){
                for(int x = 1; x < 6; x = x+1) {
                    latitude = db.getCultura(x).getCoordLat();
                    longitude = db.getCultura(x).getCoordLong();

                    googleMap.addMarker(new MarkerOptions()
                    .position(new LatLng(latitude, longitude)).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
                    .title(db.getCultura(x).getName()));
                }
            }
            if (getIntent().getIntExtra("str1", 1) == 1){
                googleMap.addMarker(new MarkerOptions()
                .position(new LatLng(double value, double value)).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN))
                .title("Sport"));
            }

            // check if map is created successfully or not
            if (googleMap == null) {
                Toast.makeText(getApplicationContext(),
                        "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                        .show();
            }
        }
    }

    @Override
    public boolean onMarkerClick(Marker arg0) {
        Intent i = new Intent(this, informazioni.class);
        //i.putExtra("str1", db.getCulturaName(arg0.getTitle()).getDescription());
        startActivity(i);
        return false;
    }

    @Override
    protected void onResume() {
        super.onResume();



        initilizeMap();
    }

}

希望有人能帮忙!谢谢

最佳答案

在您的initilizeMap()方法中,获取 map 后,请添加以下行:

googleMap.setOnMarkerClickListener(this);

关于android - onMarkerClick不起作用(v2),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19256642/

10-09 04:35