谷歌融合位置服务android

时间:2016-10-30 20:13:13

标签: android-studio android-intent service

  1. 类名是Myclass并实现类似 - >的方法LocationListener,GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener覆盖methds
  2.  private static int UPDATE_INTERVAL = 10000; // 10 sec
        private static int FATEST_INTERVAL = 5000; // 5 sec
        private static int DISPLACEMENT = 10; // 10 meters
        LocationRequest mLocationRequest;
        GoogleApiClient mGoogleApiClient;
        Location mCurrentLocation;
        String mLastUpdateTime;
        Context mContext;
    
    
    
        @Override
        public void onConnected(Bundle bundle) {
    
            mGoogleApiClient = new GoogleApiClient.Builder(mContext)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .addApi(LocationServices.API).build();
    
            createLocationRequest();
            PendingResult<Status> pendingResult = LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, (LocationListener) mContext);
    
        }
    
    
        private void createLocationRequest() {
    
            mLocationRequest = new LocationRequest();
            mLocationRequest.setInterval(UPDATE_INTERVAL);
            mLocationRequest.setFastestInterval(FATEST_INTERVAL);
            // mLocationRequest.setNumUpdates(2);
            // mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
            mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    
        }
    
        @Ov
        }
    
        @Override
        public void onConnectionFailed(ConnectionResult connectionResult) {
            stopLocationUpdates();
        }
    
        @Override
        public void onLocationChanged(Location location) {
            mCurrentLocation = location;
            DateFormat.getTimeInstance().format(new Date());
            updateUI();
    
        }
    
        public String updateUI() {
    
            if (null != mCurrentLocation) {
                Double lat = mCurrentLocation.getLatitude();
                Double lng = mCurrentLocation.getLongitude();
    
                Log.d("thhhhhhhhhhhhhhhhhhhh",lat.toString() +lng.toString());
                getCurrentlocation(mCurrentLocation);
               //setCurrentLocation(String.valueOf(lat + "," + lng));
    tLocation(mCurrentLocation);
               // arrayList.add(mCurrentLocation);
    
                return lat.toString();
            }
    
            return null;
        }
    
        private void getCurrentlocation(Location mCurrentLocation) {
    
        }
    
        public void setmCurrentLocation(Location mCurrentLocation) {
            this.mCurrentLocation = mCurrentLocation;
        }
    
        protected void stopLocationUpdates() {
            LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, (com.google.android.gms.location.LocationListener) this);
        }

    我想开发mylocation类和getlocation方法,以便返回lat loan。 如何在任何类中访问并获取lat lon ??

1 个答案:

答案 0 :(得分:0)

你可能需要创建servics,实现google locationAPI,Location listner more ..

在覆盖方法onLocationChanged中你得到lat-long现在你可以直接发送到你的服务器或其他类

我希望我的回答对你有所帮助。

如果不要再问我一次。 谢谢。