从onMarkerClick方法

时间:2015-07-21 09:27:33

标签: android google-maps google-maps-api-3 arraylist google-maps-markers

我正在处理地图应用程序,我在自定义对象POJO类的ArrayList中通过纬度和经度位置添加标记。一切正常,我能够在该位置添加标记,当我尝试从onMarkerClick内的ArrayList获取标记索引位置时出现问题(即我只通过marker.getPosition()得到标记lat / lng位置)。但我的要求是当我点击特定标记时从我的ArrayList获取确切的索引位置。

我发布了完整的MapActivity代码供您参考。

public class MainActivity extends FragmentActivity implements GoogleMap.OnMarkerClickListener {

    GoogleMap googleMap;    
    private ArrayList<LocationModel> mLocationList = new ArrayList<LocationModel> ();

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


        // Getting Google Play availability status
        int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());

        // Showing status
        if(status!=ConnectionResult.SUCCESS){ // Google Play Services are not available

            int requestCode = 10;
            Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
            dialog.show();

        }else { // Google Play Services are available           

            // Getting reference to the SupportMapFragment of activity_main.xml
            SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

            // Getting GoogleMap object from the fragment
            googleMap = fm.getMap();

            // Changing map type
            googleMap.setMapType (GoogleMap.MAP_TYPE_NORMAL);

            googleMap.setOnMarkerClickListener(this);
            // Enabling MyLocation Layer of Google Map
            googleMap.setMyLocationEnabled(true);

            mLocationList.add(new LocationModel ("11.0183", "76.9725", "Hotel Meridean"));
            mLocationList.add(new LocationModel ("13.1073", "80.2445","Hotel Lee palace"));
            mLocationList.add(new LocationModel ("12.9854", "80.1398","Hill view"));
            mLocationList.add(new LocationModel ("12.9750", "80.1347","Hotel Radisson"));
            mLocationList.add(new LocationModel ("11.6500", "78.1600","Hotel Mahabarath"));


                // Iterating through all the locations stored
                for(int i=0;i<mLocationList.size ();i++){


                    drawMarker (new LatLng (Double.parseDouble (mLocationList.get (i).getLatitude ()), Double.parseDouble (mLocationList.get (i).getLongitude ())));


                }


            CameraPosition cameraPosition = new CameraPosition.Builder()
                    .target (new LatLng (Double.parseDouble ("13.0900"), Double.parseDouble ("80.2700")))// Sets the center of the map to tamil nadu
                    .zoom (7)                   // Sets the zoom
//                    .bearing(30)                // Sets the orientation of the camera to east
//                    .tilt(30)                   // Sets the tilt of the camera to 30 degrees
                    .build ();                   // Creates a CameraPosition from the builder


            googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition), new GoogleMap.CancelableCallback () {

                @Override
                public void onFinish() {
                    CameraUpdate cu_scroll = CameraUpdateFactory.scrollBy(-300, 70);
                    googleMap.animateCamera(cu_scroll);
                }

                @Override
                public void onCancel() {
                }
            });
        }        

        googleMap.setOnMapClickListener(new OnMapClickListener() {

            @Override
            public void onMapClick(LatLng point) {      

//              Toast.makeText(getBaseContext(), ""+point, Toast.LENGTH_SHORT).show ();

            }
        }); 


        googleMap.setOnMapLongClickListener(new OnMapLongClickListener() {              
            @Override
            public void onMapLongClick(LatLng point) {
            }
        });           


    }

    private void drawMarker(LatLng point){
        // Creating an instance of MarkerOptions
        MarkerOptions markerOptions = new MarkerOptions();                  

        // Setting latitude and longitude for the marker
        markerOptions.position(point);

        // Adding marker on the Google Map
        googleMap.addMarker(markerOptions);
    }


    @Override
    public boolean onMarkerClick (Marker marker) {

        Util.showToast (MainActivity.this,""+marker.getPosition ());

        return false;
    }
}

我的LocationModel Pojo类是

public class LocationModel {

    private String latitude;

    private String longitude;

    private String hotelName;

    public String getLatitude () {
        return latitude;
    }

    public void setLatitude (String latitude) {
        this.latitude = latitude;
    }

    public String getLongitude () {
        return longitude;
    }

    public void setLongitude (String longitude) {
        this.longitude = longitude;
    }

    public LocationModel (String latitude, String longitude, String hotelName) {
        this.latitude = latitude;
        this.longitude = longitude;
        this.hotelName = hotelName;
    }

    public String getHotelName () {
        return hotelName;
    }

    public void setHotelName (String hotelName) {
        this.hotelName = hotelName;
    }
}

提前致谢。请帮助我通过宝贵的建议和答案解决我的问题。

4 个答案:

答案 0 :(得分:3)

试试这个

定义

 private Map<Integer, LatLng> marker = new HashMap();

然后

for(int i=0;i<mLocationList.size ();i++){

drawMarker (new LatLng (Double.parseDouble (mLocationList.get (i).getLatitude ()), Double.parseDouble (mLocationList.get (i).getLongitude ())));

marker.put(i,new LatLng (Double.parseDouble (mLocationList.get (i).getLatitude ()), Double.parseDouble (mLocationList.get (i).getLongitude ())));
}

然后

googleMap.setOnMapClickListener(new OnMapClickListener() {

            @Override
            public void onMapClick(LatLng point) {      

           // iterate your map and get the index by your point

              Integer value = map.get(point);

            }
        }); 

答案 1 :(得分:1)

请检查以下更改,同时将标记集索引添加为标题,此处索引是您的i值。

private void drawMarker(LatLng point , int index){
    // Creating an instance of MarkerOptions
    MarkerOptions markerOptions = new MarkerOptions();                  

    // Setting latitude and longitude for the marker
    markerOptions.position(point).title(""+index);

    // Adding marker on the Google Map
    googleMap.addMarker(markerOptions);
} 

并点击标记

      googleMap.setOnMarkerClickListener(new OnMarkerClickListener() {

        @Override
        public boolean onMarkerClick(Marker marker) {

            int index = Integer.valueOf(marker.getTitle());
            return true;
        }
    });

答案 2 :(得分:1)

您可以将Marker的地图添加到Integer,或者将Marker的地图添加到LocationModel,具体取决于您想要做什么。但看到MarkerInteger真的回答了你的问题,你可以这样做:

private Map<Marker, Integer> mMarkerMap = new HashMap<Marker, Integer>();

然后对您的drawMarker()方法进行细微更改,将ArrayList中的位置添加为参数,并将新创建的标记存储在Map中:

private void drawMarker(LatLng point, int positionInLocationList){
    // Creating an instance of MarkerOptions
    MarkerOptions markerOptions = new MarkerOptions();                  

    // Setting latitude and longitude for the marker
    markerOptions.position(point);

    // Adding marker on the Google Map
    Marker marker = googleMap.addMarker(markerOptions);

    mMarkerMap.put(marker, positionInLocationList);
}

然后,在您拨打drawMarker()的地方,您只需将索引添加到呼叫中:

drawMarker (new LatLng (Double.parseDouble (mLocationList.get (i).getLatitude ()), Double.parseDouble (mLocationList.get (i).getLongitude ())), i);

onMarkerClick()中,您只需在地图中查找Marker即可将您的索引编入mLocationList

int index = mMarkerMap.get(marker);

答案 3 :(得分:0)

你可以使用HashMap而不是ArrayList,而HashMap的键就是这个类:

public class LocationModelKey {

private String latitude;

private String longitude;

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    LocationModelKey that = (LocationModelKey) o;

    if (latitude != null ? !latitude.equals(that.latitude) : that.latitude != null)
        return false;
    return !(longitude != null ? !longitude.equals(that.longitude) : that.longitude != null);

}

@Override
public int hashCode() {
    int result = latitude != null ? latitude.hashCode() : 0;
    result = 31 * result + (longitude != null ? longitude.hashCode() : 0);
    return result;
}}

然后通过特定键获取对象(新的LocationModelKey(“你的lat”,“你的长”);