简体   繁体   English

Android 实时位置跟踪

[英]Android Real Time Location Tracking

Hello everyone I am developing two apps one for Admin and second for its employees.大家好,我正在开发两个应用程序,一个是为管理员开发的,另一个是为员工开发的。 Now i want to track the employee device using this app the location is send to server when location of employee device changed.Location is send only when location changed.现在我想使用此应用程序跟踪员工设备,当员工设备的位置更改时,位置将发送到服务器。仅当位置更改时才发送位置。 how i can send location of device to server when location is changed??位置更改时如何将设备的位置发送到服务器? second i want to show the location of employee on admin app map as it is changing its location.(May i send Location co ordinates from server to admin app through GCM or any other technique?)其次,我想在管理应用程序地图上显示员工的位置,因为它正在更改其位置。(我可以通过 GCM 或任何其他技术将位置坐标从服务器发送到管理应用程序吗?)

Hey, down voters can you explain what is wrong in this question?嘿,选民你能解释一下这个问题有什么问题吗? If you have a Answer to the question the give otherwise try to understand the questions.如果您对问题有答案,请尝试理解问题。 please help Thanks in advance..请帮助提前谢谢..

you can implement locationListener with service, you can req api from server and send data in any of form, json, xml and once you got the co-ordinates you can show them to the map.您可以使用服务实现locationListener,您可以从服务器请求api并以任何形式发送数据,json,xml,一旦获得坐标,您就可以将它们显示到地图上。

LocationListener 位置监听器

android-location-based-services-example android-location-based-services-example

Hope these can help you.希望这些能帮到你。

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;
 }
}

**Use this to get location ** **使用它来获取位置**

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
 }
 }

I know my response is a little bit late.我知道我的回答有点晚了。

I will explain my answer using google maps services and firebase real-time database and Geo-Fire for easy location uploading to the cloud database.我将使用谷歌地图服务和 firebase 实时数据库以及 Geo-Fire 来解释我的答案,以便将位置上传到云数据库。

  1. Get the real-time location -获取实时位置——

     @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. Adding the real-time location to the cloud database using Geo-Fire使用 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));

    } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM