android - 每个分钟标记都平滑移动,但不会删除 android map 上的旧标记

标签 android google-maps google-maps-markers

  1. 我正在从共享首选项中获取每分钟经纬度,并在 map 上显示。

  2. 标记每分钟都在完美移动,但没有删除旧标记(它显示每分钟新标记。

  3. 但是关闭和打开 map fragment 它完美地显示了单个标记。
  4. 请帮我解决这个问题。我也试过 Marker.Remove

  5. 我在 OnLocation Changed Method 中调用了以下方法。

    /* 在UI上显示位置的方法 */

    private void displayLocation()
    {
    
    
        try
        {
    
            mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
    
            if (mLastLocation != null)
            {
                double latitude = mLastLocation.getLatitude();
                double longitude = mLastLocation.getLongitude();
    
    
    
                // get user data from session
                HashMap<String, String> user = session.getGPSPING();
    
                // UserLat
                String  LatLongUser="";
                LatLongUser = user.get(SessionManagerFor_Register.KEY_LATLONG);
    
    
    
    
                if(!LatLongUser.equals(""))
                {
    
                    Log.i(" PING on MAP LatLong", LatLongUser);
    
                    String[] LanlongArr = LatLongUser.split("//");
                    List<String> Lanlonglist1 = Arrays.asList(LanlongArr);
    
                    int length = Lanlonglist1.size();
    
                    arraylist_DetailLineWalker = new ArrayList<String(length);
    
                    for (int i = 0; i < length; i++)
                    {
    
    
                        arraylist_DetailLineWalker.add(Lanlonglist1.get(i));
                    }
    
                    if(arraylist_DetailLineWalker!=null)
                    {
    
                        for (int i = 0; i < arraylist_DetailLineWalker.size(); i++)
                        {
                            try {
                                String Val = arraylist_DetailLineWalker.get(i).toString();
                                //Log.i(" Validation Id",Val);
                                VALUE_ARRAY_STRING = Val.toString().split("::");
    
                                LatLong_DataSaveTable = VALUE_ARRAY_STRING[0].toString();
    
    
                                System.out.println("checking STarted" + LatLong_DataSaveTable);
    
                                String[] latlong = LatLong_DataSaveTable.split(",");
                                double latitude1 = Double.parseDouble(latlong[0]);
                                double longitude2 = Double.parseDouble(latlong[1]);
    
                                //To hold location
                                LatLng latLng1 = new LatLng(latitude1, longitude2);
                                //To create marker in map
                                MarkerOptions markerOptionsLineWalker = new MarkerOptions();
                                markerOptionsLineWalker.position(latLng1); //setting position
                                markerOptionsLineWalker.draggable(true); //Making the marker draggable
                                markerOptionsLineWalker.title("Walker Location");
    
                                markerOptionsLineWalker.icon(BitmapDescriptorFactory.fromResource(R.drawable.walker_outof_fence_icon_red));
    
    
                                Marker marker1 = googleMap.addMarker(markerOptionsLineWalker);
                                if (marker1 != null)
                                {
                                    marker1.remove();
                                }
                                //adding marker to the map
                                googleMap.addMarker(markerOptionsLineWalker);
                                // marker1.setPosition(latLng1);
                                Log.i(TAG, "Walker PING Added.............................");
    
                            } catch (NumberFormatException e) {
                                e.printStackTrace();
                            }
    
                        }
    
                    }
                    else
                    {
                        Log.i("MAP NEwLatLong","TOTAL ARRY LIST NULLL");
                    }
    
    
                }
                else
                {
                    Log.i("MAP NEwLatLong","Null Not LatLong");
    
    
                }
    
    
            }
            else
            {
    
                Log.i("Location EXception","Couldn't get the location. Make sure location is enabled on the device");
                // can't get location
                // GPS or Network is not enabled
                // Ask user to enable GPS/network in settings
                gps.showSettingsAlert();
            }
    
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    
    
    }
    

最佳答案

试试这个....

  Marker now;
  @Override
  public void onLocationChanged(Location location) {

  if(now != null){
        now.remove();   //if the marker is already added then remove it
    }

   // Getting latitude of the current location
   double latitude = location.getLatitude();

   // Getting longitude of the current location
    double longitude = location.getLongitude();

  // Creating a LatLng object for the current location
    LatLng latLng = new LatLng(latitude, longitude);
    now = googleMap.addMarker(new MarkerOptions().position(latLng)));
 }

要引用,请访问此...

https://stackoverflow.com/a/16312869/6385873

关于android - 每个分钟标记都平滑移动,但不会删除 android map 上的旧标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38196013/

相关文章:

android - getSupportActionBar 标题颜色

javascript - 如何从 google map api 更改 map 位置?

android - 如何在外部 Google map 应用程序中加载 Android Direction API 结果?

javascript - 更新用户生成的标记的搜索结果

Android 位图裁剪中心

java - HttpURLConnection 在 Android 上的哪里存储 session ID cookie?

android - osmdroid 中没有显示 map

php - 谷歌地图 api 访问 iPhone 的可能性吗? (在没有 UIWebView 的情况下处理 javascript)

android - Google Maps Android API v2,标记位置错误

html - 在 html5 单页应用程序上使用谷歌地图时如何管理内存