2016-10-25 1 views
-3

는 내가 현재 위치를 트리거 할 Google지도 API에서 매 30 초

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

+0

에 오신 것을 환영합니다, 간격이 방법을 실행하려고 할 경우 , 당신은 [ask]를 볼 수 있습니다. 더 구체적으로하려고하면 Google 서비스를 사용하여 GPS 위치를 원합니다. Google지도 API는지도 활동 만 제공합니다 (내가 틀린 것을 제외하고). – AxelH

+0

또한, 우리가 가지고있는 것에서 대답하기 위해 LocationListener를 사용하여 매 30 초마다 자동으로 이벤트를 묻습니다. 등록 방법의 매개 변수 중 하나가 위치 사이의 시간. 이것은 더 많은 전력 효율이 될 것입니다. 이것은 Google 서비스를 사용하지 않을 것이고, 이것은 당신이 요청한 LocationListener만을 사용할 것입니다.하지만 이것은 – AxelH

답변

0

그냥 자바 클래스를 Timer

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() { 
      getCurrentLocation // Your method from above .. 
     } 
    }, delay, interval); 
+0

이 감사합니다. –

+0

하지만 내가 움직이기 시작하면 같은 경도와 위도 값을 표시합니다 .... .pls @ 코 페코 도움 –

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)); 
관련 문제