2016-11-09 3 views
-2

私はGoogleマップを使用してアプリを開発していますが、私は彼の管理者に現在のドライバを表示します。彼の管理者にドライバの現在地をどのように表示するのですか?どのように私はドライバの現在の場所を追跡し、彼の管理者に表示するのですか?アンドロイドスタジオのGoogleマップで、1人のユーザーの現在地を他の場所に表示するにはどうすればよいですか?

次のコードはドライバの現在の位置のみを表示しています。このドライバの現在の位置を管理者に表示するにはどうすればよいですか?

//java code 
    public class LocationActivity extends FragmentActivity implements 
      com.google.android.gms.location.LocationListener, 
      GoogleApiClient.ConnectionCallbacks, 
      GoogleApiClient.OnConnectionFailedListener { 

     private static final String TAG = "LocationActivity"; 
     private static final long INTERVAL = 1000 * 60 * 1; //1 minute 
     private static final long FASTEST_INTERVAL = 1000 * 60 * 1; // 1 minute 

     private static final float SMALLEST_DISPLACEMENT = 0.25F; //quarter of a meter 
     Button btnFusedLocation; 
     TextView tvLocation; 
     LocationRequest mLocationRequest; 
     GoogleApiClient mGoogleApiClient; 
     Location mCurrentLocation; 
     String mLastUpdateTime; 
     GoogleMap googleMap; 

     private double longitude; 
     private double latitude; 
     private ArrayList<LatLng> points; //added 
     Polyline line; //added 

     protected void createLocationRequest() { 
      mLocationRequest = new LocationRequest(); 
      mLocationRequest.setInterval(INTERVAL); 
      mLocationRequest.setFastestInterval(FASTEST_INTERVAL); 
      mLocationRequest.setSmallestDisplacement(SMALLEST_DISPLACEMENT); //added 
      mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
     } 
     /* 
     protected void createLocationRequest() { 
      mLocationRequest = new LocationRequest(); 
      mLocationRequest.setInterval(INTERVAL); 
      mLocationRequest.setFastestInterval(FASTEST_INTERVAL); 
      mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
     } */ 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      Log.d(TAG, "onCreate ..............................."); 

      points = new ArrayList<LatLng>(); 
      //show error dialog if GoolglePlayServices not available 
      if (!isGooglePlayServicesAvailable()) { 
       finish(); 
      } 
      createLocationRequest(); 
      mGoogleApiClient = new GoogleApiClient.Builder(this) 
        .addApi(LocationServices.API) 
        .addConnectionCallbacks(this) 
        .addOnConnectionFailedListener(this) 
        .build(); 

      setContentView(R.layout.activity_location_google_map); 
      SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager() 
        .findFragmentById(R.id.map); 
      googleMap = fm.getMap(); 
      googleMap.getUiSettings().setZoomControlsEnabled(true); 

     /* if (getIntent().getExtras() != null) { 
       final Bundle bundle = getIntent().getBundleExtra("LOCATION"); 
       latitude = bundle.getDouble("LATITUDE"); 
       longitude = bundle.getDouble("LONGITUDE"); */ 

     } 

     @Override 
     public void onStart() { 
      super.onStart(); 
      Log.d(TAG, "onStart fired .............."); 
      mGoogleApiClient.connect(); 
     } 

     @Override 
     public void onStop() { 
      super.onStop(); 
      Log.d(TAG, "onStop fired .............."); 
      mGoogleApiClient.disconnect(); 
      Log.d(TAG, "isConnected ...............: " + mGoogleApiClient.isConnected()); 
     } 

     private boolean isGooglePlayServicesAvailable() { 
      int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); 
      if (ConnectionResult.SUCCESS == status) { 
       return true; 
      } else { 
       GooglePlayServicesUtil.getErrorDialog(status, this, 0).show(); 
       return false; 
      } 
     } 

     @Override 
     public void onConnected(Bundle bundle) { 
      Log.d(TAG, "onConnected - isConnected ...............: " + mGoogleApiClient.isConnected()); 
      startLocationUpdates(); 
     } 

     protected void startLocationUpdates() { 
      PendingResult<Status> pendingResult = LocationServices.FusedLocationApi.requestLocationUpdates(
        mGoogleApiClient, mLocationRequest, this); 
      Log.d(TAG, "Location update started ..............: "); 
     } 

     @Override 
     public void onConnectionSuspended(int i) { 

     } 

     @Override 
     public void onConnectionFailed(ConnectionResult connectionResult) { 
      Log.d(TAG, "Connection failed: " + connectionResult.toString()); 
     } 

     @Override 
     public void onLocationChanged(Location location) { 
      Log.d(TAG, "Firing onLocationChanged.............................................."); 
      mCurrentLocation = location; 
      mLastUpdateTime = DateFormat.getTimeInstance().format(new Date()); 
      addMarker(); 

      longitude = location.getLongitude(); 
      latitude = location.getLatitude(); 
      LatLng latLng = new LatLng(latitude, longitude); //you already have this 
      points.add(latLng); //added 
      redrawLine(); 
     } 

     private void addMarker() { 
      MarkerOptions options = new MarkerOptions(); 

      // following four lines requires 'Google Maps Android API Utility Library' 
      // https://developers.google.com/maps/documentation/android/utility/ 
      // I have used this to display the time as title for location markers 
      // you can safely comment the following four lines but for this info 
      IconGenerator iconFactory = new IconGenerator(this); 
      iconFactory.setStyle(IconGenerator.STYLE_PURPLE); 
      options.icon(BitmapDescriptorFactory.fromBitmap(iconFactory.makeIcon(mLastUpdateTime))); 
      options.anchor(iconFactory.getAnchorU(), iconFactory.getAnchorV()); 

      LatLng currentLatLng = new LatLng(mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude()); 
      options.position(currentLatLng); 
      Marker mapMarker = googleMap.addMarker(options); 
      long atTime = mCurrentLocation.getTime(); 
      mLastUpdateTime = DateFormat.getTimeInstance().format(new Date(atTime)); 
      mapMarker.setTitle(mLastUpdateTime); 
      Log.d(TAG, "Marker added............................."); 
      googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLatLng, 
        13)); 
      Log.d(TAG, "Zoom done............................."); 
     } 

     @Override 
     protected void onPause() { 
      super.onPause(); 
      stopLocationUpdates(); 
     } 

     protected void stopLocationUpdates() { 
      LocationServices.FusedLocationApi.removeLocationUpdates(
        mGoogleApiClient, this); 
      Log.d(TAG, "Location update stopped ......................."); 
     } 

     @Override 
     public void onResume() { 
      super.onResume(); 
      if (mGoogleApiClient.isConnected()) { 
       startLocationUpdates(); 
       Log.d(TAG, "Location update resumed ....................."); 
      } 
     } 

     // for map line 
     private void redrawLine(){ 

      googleMap.clear(); //clears all Markers and Polylines 
      PolylineOptions options = new PolylineOptions().width(5).color(Color.BLUE).geodesic(true); 
      for (int i = 0; i < points.size(); i++) { 
       LatLng point = points.get(i); 
       options.add(point); 
      } 
      addMarker(); //add Marker in current position 
      line = googleMap.addPolyline(options); //add Polyline 

     } 

    } 
+0

このためにバックエンドサーバーが必要です。 –

+0

まず場所を保存して表示しますか? – rohiH

答えて

0

これが解決策の一つ
1.ユーザの現在位置を取得して、サーバー上でそれを更新している可能性があります。
2.バックエンドサーバーにFirebaseを使用します。これにより、多くの作業が簡単になります。ユーザーの場所をfirebaseに保存します。
3.管理パネルで、firebaseからユーザーの場所を取得します。
通知を使用して、ユーザーがfirebaseで更新したら、そのユーザーの現在の場所についてadminに通知できます。

+0

申し訳ありませんどのような例を参照してください? – rohiH

関連する問題