Android实时位置跟踪

时间:2014-11-25 06:08:57

标签: android android-activity

大家好我正在为Admin开发两个应用程序,为员工开发第二个应用程序。现在我想使用此应用程序跟踪员工设备,当员工设备的位置发生变化时,该位置将发送到服务器。仅在位置更改时才发送位置。如何在位置发生变化时将设备的位置发送到服务器?  第二,我想在管理员应用程序地图上显示员工的位置,因为它正在改变其位置。(我可以通过GCM或任何其他技术将位置从服务器发送到管理员应用程序吗?)

嘿,沮丧的选民可以解释这个问题的错误吗?如果您对问题有答案,那么请尽量理解问题。 请帮忙 提前谢谢..

3 个答案:

答案 0 :(得分:0)

你可以用服务实现locationListener,你可以从服务器req api并以任何形式,json,xml发送数据,一旦你得到坐标,你就可以将它们显示到地图上。

LocationListener

android-location-based-services-example

希望这些可以帮到你。

答案 1 :(得分: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
 }
 }

答案 2 :(得分:0)

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

我将使用Google Maps Services和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) {
         // add the code to get the permission
         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()); // plot the realtime location on the map
         mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
         mMap.animateCamera(CameraUpdateFactory.zoomTo(11));      
         addToDataBase(latLng);
     }
    

    }

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

    public void addtoDataBase(LatLng location){
     //unique name work like a promary key
     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));
    

    }