onMarkerClick不工作(v2)

时间:2013-10-08 19:30:55

标签: android google-maps

我在onMarkerClick上遇到了一些麻烦

基本上现在我只想在之前创建的任何标记在单击时启动相同的活动时。我稍后会实现对标记的过滤。

我现在得到的是没有错误。单击标记时,没有任何反应。

这是我的来源:

我循环遍历数据库以填充地图,然后显示标记

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();
    }

}

希望有人可以帮忙!谢谢

1 个答案:

答案 0 :(得分:35)

initilizeMap()方法中,请在获得地图后添加此行:

googleMap.setOnMarkerClickListener(this);