我想在google maps api中每30秒触发一次当前位置

时间:2016-10-25 06:11:52

标签: android

wanted add in this method
 private void getCurrentLocation() {
        //Creating a location object
        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) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        Location location = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
        if (location != null) {
            //Getting longitude and latitude
            longitude = location.getLongitude();
            latitude = location.getLatitude();

            //moving the map to location
            moveMap();
        }
    }

2 个答案:

答案 0 :(得分:0)

如果您只想间隔运行此方法,请尝试java class FragmentManager fm = getFragmentManager();// If you're in an activity. FragmentManager fm = getSupportFragmentManager();// If you're already inside another fragment YourFragment yfObj = new YourFragment(); fm.beginTransaction().replace(R.id.fragmentContainer, yfObj).commit();

Timer

答案 1 :(得分:0)

LoactionListener mLocationListener;
double lat,lng;


inside your OnCreateMethod :




mLocationListener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                lat = location.getLatitude();
                lng = location.getLongitude();

            }

            @Override
            public void onStatusChanged(String s, int i, Bundle bundle) {

            }

            @Override
            public void onProviderEnabled(String s) {

            }

            @Override
            public void onProviderDisabled(String s) {

            }
        };



if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) || mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
            try {

                if (ActivityCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    // TOD
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.


                    // Not implementing Permission Request Measures as User Should not know he is being tracked

                }


                Criteria c = new Criteria();
                c.setAccuracy(Criteria.ACCURACY_FINE);
                c.setAltitudeRequired(false);
                c.setBearingRequired(false);
                c.setSpeedRequired(false);


                // mLocationManager.requestSingleUpdate(c, mLocationListener, getMainLooper());

                mLocationManager.requestLocationUpdates(REFRESH_TIME, REFRESH_DISTANCE, c, mLocationListener, getMainLooper()); //Check if this can give us location updates on change in locations


            } catch (Exception e) {
                Log.d(TAG, " Exception e" + e.toString());
            }
        }else{
            Log.d(TAG,"Location permission are disabled in service");
        }



now using the Koperko solution
int delay = 5000;   // delay for 5 sec.
int interval = 30000;  // iterate every 30 sec.
Timer timer = new Timer();

timer.scheduleAtFixedRate(new TimerTask() {
      public void run() {
          mFragment.getMapAsync(Context);                         // The object of your own map support fragment
      }
  }, delay, interval);



inYour onMapReady method
use
LatLng mLatLng=new LatLng(lat,lng);
Marker marker = mGoogleMap.addMarker(new MarkerOptions().position(mLatLng));