2016-12-26 55 views
3

你好,我正在使用谷歌地图LocationListener。我能点之间绘制路径中使用融合APIAndroid谷歌地图在移动时绘制路径

protected void createLocationRequest() { 
     mLocationRequest = new LocationRequest(); 
     mLocationRequest.setInterval(1000 * 60 * 1); 
     mLocationRequest.setFastestInterval(1000 * 60 * 1); 
              } 
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 

这里我绘制的路径:

public void onLocationChanged(Location location) { 
     LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude()); 

       MarkerOptions markerOptions = new MarkerOptions(); 
       markerOptions.position(latLng); 
       routePoints.add(latLng); 
       Polyline route = mGoogleMap.addPolyline(new PolylineOptions() 
         .width(5) 
         .color(Color.BLUE) 
         .geodesic(false) 
         .zIndex(3)); 
       route.setPoints(routePoints); 

              } 

的一点是,我需要同时用户移动绘制实时路径和停止当用户停止时,无论间隔时间为LocationRequest

回答

4

试试这个方法,这样您就可以根据需要决定和更改这些值。需要多少时间间隔以及多少分钟距离才能调用此方法。没有互联网的工作。

private LocationManager locationManager; 
    private android.location.LocationListener myLocationListener; 

    public void checkLocation() { 

     String serviceString = Context.LOCATION_SERVICE; 
     locationManager = (LocationManager) getSystemService(serviceString); 


     if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      return; 
     } 


     myLocationListener = new android.location.LocationListener() { 
      public void onLocationChanged(Location locationListener) { 

       if (isGPSEnabled(YourActivityName.this)) { 
        if (locationListener != null) { 
         if (ActivityCompat.checkSelfPermission(YourActivityName.this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(YourActivityName.this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
          return; 
         } 

         if (locationManager != null) { 
          location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
          if (location != null) { 
           latitude = location.getLatitude(); 
           longitude = location.getLongitude(); 
          } 
         } 
        } 
       } else if (isInternetConnected(YourActivityName.this)) { 
        if (locationManager != null) { 
         location = locationManager 
           .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
         if (location != null) { 
          latitude = location.getLatitude(); 
          longitude = location.getLongitude(); 
         } 
        } 
       } 



      } 

      public void onProviderDisabled(String provider) { 

      } 

      public void onProviderEnabled(String provider) { 

      } 

      public void onStatusChanged(String provider, int status, Bundle extras) { 

      } 
     }; 

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, myLocationListener); // here the min time interval and min distance 
} 

isInternetConnected方法

public static boolean isInternetConnected(Context ctx) { 
     ConnectivityManager connectivityMgr = (ConnectivityManager) ctx 
       .getSystemService(Context.CONNECTIVITY_SERVICE); 
     NetworkInfo wifi = connectivityMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI); 
     NetworkInfo mobile = connectivityMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); 
     // Check if wifi or mobile network is available or not. If any of them is 
     // available or connected then it will return true, otherwise false; 
     if (wifi != null) { 
      if (wifi.isConnected()) { 
       return true; 
      } 
     } 
     if (mobile != null) { 
      if (mobile.isConnected()) { 
       return true; 
      } 
     } 
     return false; 
    } 

isGpsEnabled方法

public boolean isGPSEnabled(Context mContext) { 
     LocationManager locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE); 
     return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 
    } 

逻辑:检查lastlocationcurrantLocation通过保持变数,而且如果是的,它意味着你没有,如果移动不绘制路径

+0

谢谢你,但我想没有时间间隔我的意思是画线,而用户后,移动不是一个方法间隔,由于电池问题,我不能使它间隔很小。我只想在用户移动时使用一个程序。 – Radwa

+0

@Radwa请注意'locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000,1,myLocationListener);'这并不意味着它在每秒意味着它被调用,差异需要至少1秒和1米的通话该方法再次。如果位置不再更改它永远不会被调用更多 –

+0

我得到你的想法我的问题是我做了间隔1分钟它绘制的路径,但忽略点我检查在不到分钟即如果我检查在A,然后转到B,然后C从点A画到点C忽略点b,因为它不到分钟 – Radwa

0
在你类

定义谷歌API的这个方法:

private GoogleMap.OnMyLocationChangeListener myLocationChangeListener = new GoogleMap.OnMyLocationChangeListener() { 
    @Override 
    public void onMyLocationChange(Location location) { 
    // make your code to draw path , and refresh the map to not hve a ot of path . 

    } 

了解更多详细信息请参见here

+0

谢谢但它已被弃用,并且它也需要时间间隔 – Radwa