繁体   English   中英

用户将标记添加到地图上后,如何访问标记?

[英]How do I access markers when they are added on the map by the user?

当用户单击地图上的某个位置时,将打开一个对话框警告对话框,提示用户是否要添加标记(如果是,则添加标记)。 我正在尝试编写另一种方法,如果用户通过此标记,则标记会更改颜色。

我在编写标记检查部分时遇到了麻烦。

我的代码:

public void onMapClick(){
    map.setOnMapClickListener(new OnMapClickListener() {
        @Override
        public void onMapClick(LatLng latlng) {

            dialogBox(latlng);
        }
    });
}
private void dialogBox(final LatLng latlng) {
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MapActivity.this);

    alertDialogBuilder.setTitle("Add Marker");

    alertDialogBuilder
    .setMessage("Add marker?").setCancelable(false)
    .setPositiveButton("yes", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {

            double dialogLat = latlng.latitude;
            double dialogLng = latlng.longitude;

            dialogMarker = map.addMarker(new MarkerOptions()
        .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN))
            .position(latlng).title("dialog maker"));

            //if location hit, places marker around that area
            hitLocation(dialogLat,dialogLng);

        }
    })
    .setNegativeButton("no", new DialogInterface.OnClickListener() {

        //cancels
        }
    }); // create alert dialog show it
}
public void hitLocation( final double dialogLat, final double dialogLng){
    LocationListener ll = new LocationListener() {

        @Override
        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub

            double lat = location.getLatitude();
            double lon = location.getLongitude();

            final LatLng currentLatLon = new LatLng(lat, lon);


            //tracks weather or not person hit the location
            if((lat > (dialogLat- 0.0001) && lat < (dialogLat+ 0.0001)) &&
            (lon > (dialogLon - 0.0001) && lon < (dialogLon + 0.0001))){
                dialogMarker = map.addMarker(new MarkerOptions().position(currentLatLon)
            }
        } // other location listener methods

它可以达到我在警报对话框的onClick方法内传递hitLocation(doubleLat,doubleLng)的位置。 我认为应该发生的是,当我在地图上放置标记时,hitLocation应该检查我是否确实按了用户设置的位置。

真的不清楚这是如何结合在一起的(至少对我而言)。 例如,我什至看不到hitLocation(double,double)的调用方式,但确实看到了几个问题。

首先。 这是错字吗?

    if((lat > (dialogLat- 0.0001) && lat < (dialogLng+ 0.0001)) 

你的意思是

    if((lat > (dialogLat- 0.0001) && lat < (dialogLat+ 0.0001)) 

对话** Lat ** vs对话** Lng **

我认为您最好使用这样的方法来检查坐标是否接近。

Math.abs(value1 - value2) < someTolerance

纬度和经度并不总是正的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM