【问题标题】:How do I access markers when they are added on the map by the user?用户在地图上添加标记时如何访问它们?
【发布时间】:2014-08-11 04:01:17
【问题描述】:

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

我在编写其中的标记检查部分时遇到问题。

我的代码:

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();但我们所看到的只是 hitLocation(double, double); 的方法签名; ...我认为您需要提供更多信息和更多代码
  • 使用正确的 hitLocation 参数进行编辑。对于 hitLocation,我想输入用户在触摸地图时留下的 lat 和 lng 坐标,并且用户绕过该区域,然后在用户之前触摸的区域上应显示一个红色标记。跨度>

标签: java android google-maps google-maps-api-2


【解决方案1】:

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

首先。这是一个错字吗?

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

你是说

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

dialog**Lat** vs dialog**Lng**

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

Math.abs(value1 - value2) < someTolerance

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

【讨论】:

  • 对其进行了编辑,并解释了我希望 hitLocation 做什么并修复错字,我将尝试实现 Math.abs
猜你喜欢
  • 2018-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-14
  • 2014-09-22
  • 1970-01-01
  • 2018-06-16
  • 1970-01-01
相关资源
最近更新 更多