2017-08-03 35 views
0

我试图检查用户是否处于结束位置(经纬度对象)的边界,即使当前位置=结束位置,contains函数总是返回false。所有坐标都正确更新。我是否错误地使用了LatLngBounds?任何想法如何解决它或使用界限的替代?LatLng Bounds contains()总是返回false

protected void changeStep() throws JSONException { 

    Log.i(TAG, " =========== changeStep ========= "); 

    int i = 0; 
    while (i < routes.size()) { 

     //coordinates for end location 
     Double latEndLoc = routes.get(i).endLocation.lat; 
     Double lngEndLoc = routes.get(i).endLocation.lng; 
     LatLng endLoc = new LatLng(latEndLoc,lngEndLoc); 

     //setting bounds for end location 
     LatLngBounds.Builder builder = new LatLngBounds.Builder(); 
     builder.include(endLoc); 
     LatLngBounds bounds = builder.build(); 

     Log.i("endLoc", endLoc.toString()); 
     Log.i("bounds center", bounds.getCenter().toString()); 
     Log.i("currentLoc", currentLocation.toString()); 


     //checking if users current location is within bounds of end location 
      if (bounds.contains(currentLocation)) { 

       Log.i(TAG, " =========== enteredIfBranch ========= "); 

       setText(i + 1); 
       return; 

      } 


     i++; 

    } 

} 
+0

除了这个事实,你不能做任何事情,同积1分,你怎么确定你考虑终点的“界限”?你至少需要2分 – tyczj

回答

1

至少需要两个位置。

LatLngBounds.Builder builder = new LatLngBounds.Builder(); 
    builder.include(somewhere); 
    builder.include(endLoc); 
    LatLngBounds bounds = builder.build(); 
+0

无论如何要建立一个位置的边界?或从一个位置获得两点? –

+0

@SaritMafouda,甚至没有意义你刚刚问了什么。这听起来像你正在尝试创建一些geofence,所以也许你应该看看geofence api https://developer.android.com/training/location/geofencing.html – tyczj

+0

我应该澄清我的问题。我正在创建一个导航应用程序,并且一旦用户到达特定位置,我正尝试切换到下一个方向步骤。但是,我希望位置具有半径/范围,因为用户不总是处于确切的拉伸点。看起来geofence可能是我需要的。 –

0
builder.include(endLoc); 

这应该是 “包括()”:

builder.including(endLoc);