安卓实时位置跟踪

3
大家好,我正在开发两个应用程序,一个是给管理员使用的,另一个是给员工使用的。现在我想使用这个应用程序跟踪员工设备,当员工设备的位置发生变化时,位置将被发送到服务器。只有当位置发生变化时,才会发送位置信息。那么当位置发生变化时,我如何将设备的位置发送到服务器?其次,我想在管理员应用程序地图上显示员工的位置,并随着位置的变化而更新。(我可以通过GCM或其他技术从服务器向管理员应用程序发送位置坐标吗?)
嘿,投票者们能否解释一下这个问题有什么问题吗?如果你有问题的答案,请回答,否则请理解问题。
请帮忙,谢谢。

你可以使用LocationListener接口来发送位置改变的信息。第二个选项是可以使用Google地图API。对于细节我不是很清楚,抱歉。只是提醒一下。 - therealprashant
请查看以下链接:http://developer.android.com/reference/android/location/LocationListener.html - therealprashant
如何在位置更改方法中设置50米的距离? - user3820207
你的意思是我必须设置 if(location = 50m){ // 将位置发送到服务器} 吗? - user3820207
是的,我猜是这样!但请用合适的方式解释你的问题!祝好! - therealprashant
显示剩余2条评论
3个回答

0
public LatLng getLocation()
{
 // Get the location manager
 MyLocationListener myLocListener = new MyLocationListener();
 LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
 Criteria criteria = new Criteria();
 String bestProvider = locationManager.getBestProvider(criteria, false);
 Location location = locationManager.getLastKnownLocation(bestProvider);
 locationManager.requestLocationUpdates(bestProvider,timeAfteryougetUpdate, minDistanceAfterYougetupdate, myLocListener);
 Double lat,lon;
 try {
   lat = location.getLatitude ();
   lon = location.getLongitude ();
   return new LatLng(lat, lon);
 }
 catch (NullPointerException e){
     e.printStackTrace();
   return null;
 }
}

**使用此代码获取位置信息**

private class MyLocationListener implements LocationListener
  {
@Override
public void onLocationChanged(Location loc)
  {
  if (loc != null)
  {
     // Do something knowing the location changed by the distance you requested
     methodThatDoesSomethingWithNewLocation(loc);
  }
 }

 @Override
 public void onProviderDisabled(String arg0)
 {
   // Do something here if you would like to know when the provider is disabled by the user
 }

 @Override
 public void onProviderEnabled(String arg0)
 {
    // Do something here if you would like to know when the provider is enabled by the   user
  }

  @Override
 public void onStatusChanged(String arg0, int arg1, Bundle arg2)
 {
  // Do something here if you would like to know when the provider status changes
 }
 }

假设我需要设置25米,当25米的位置发生变化时,我将通过JSON将位置发送到服务器。现在,我该如何在位置变化时设置参数为25米? - user3820207
locationManager.requestLocationUpdates(bestProvider, timeAfteryougetUpdate, 25, myLocListener); - Arun Salaria
还有一件事,当位置改变时,位置将通过简单的JSON方法发送,还是我需要为此目的使用任何服务? - user3820207
不需要在此处调用requestLocationUpdates,只需从Location获取latLng并上传它们即可。此方法将在由您指定的距离或时间之后自动调用。 - Arun Salaria
现在你能告诉我问题的第二部分了吗?如何将这个位置从服务器发送到管理员应用程序?我可以使用GCM吗? - user3820207
显示剩余11条评论

0

你可以使用服务实现LocationListener,你可以从服务器请求API并以json、xml或任何形式发送数据,一旦你得到了坐标,就可以将它们显示在地图上。

LocationListener

android-location-based-services-example

希望这些对你有所帮助。


如果员工连续更改其位置达10公里,则持续使用服务将使应用程序变慢。 - user3820207
你需要在什么时间和距离上调用这个方法。lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 35000, 10, this.locationListener); - Amit Sinha

0

我知道我的回复有点晚了。

我将使用Google地图服务、Firebase实时数据库和Geo-Fire来解释我的答案,以便轻松上传位置到云数据库。

  1. 获取实时位置 -

     @Override public void onMapReady(GoogleMap googleMap) {
     mMap = googleMap;
     locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
     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;
     }
     buildGoogleApiClient();
     mMap.setMyLocationEnabled(true);
    

    }

     protected synchronized void buildGoogleApiClient(){
     mGoogleApiClient = new GoogleApiClient.Builder(this)
             .addConnectionCallbacks(this)
             .addOnConnectionFailedListener(this)
             .addApi(LocationServices.API)
             .build();
     mGoogleApiClient.connect();
    

    }

     @Override public void onConnected(@Nullable Bundle bundle) {
     mLocationRequest = new LocationRequest();
     mLocationRequest.setPriority(LocationRequest.PRIORITY_LOW_POWER);
     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;
     }
     LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
    

    }

     @Override     public void onLocationChanged(Location location) {
     if(getApplicationContext() != null) {
         mlasLocation = location;
         LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude()); // 在地图上绘制实时位置
         mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
         mMap.animateCamera(CameraUpdateFactory.zoomTo(11));      
         addToDataBase(latLng);
     }
    

    }

  2. 使用Geo-Fire将实时位置添加到云数据库中

    public void addtoDataBase(LatLng location){
     //唯一名称类似于主键
     String uid = "119-userID";
     final DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("SOSRequests");
     GeoFire geoFire = new GeoFire(databaseReference);
     geoFire.setLocation(uid, new GeoLocation(location.latitude, location.longitude));
    

    }


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接