2013-11-21 4 views
3
public class NativeGeolocation extends Plugin { 
    public long maximumAge = 1000 * 30; // ms 
    public long timeout = 1000 * 30; // ms 
    public Location lastPosition = null; 
    public static final String ACTION_GETCURRENTPOSITION="getCurrentPosition"; 
    protected String callbackId = null; 

    @Override 
    public PluginResult execute(String action, JSONArray data, String callbackId) 
    { 
    JSONObject options = data.optJSONObject(0); 
    Log.i("Myactivity","options : "+options); 
    this.timeout = timeout; 

    this.callbackId = callbackId; 
    Log.i("Myactivity","callbackId : "+this.callbackId); 
    PluginResult result = new PluginResult(Status.NO_RESULT, callbackId); 
    result.setKeepCallback(true); 
    final LocationManager locationManager = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE); 

    Criteria criteria = new Criteria(); 

    String provider = locationManager.getBestProvider(criteria,false); 
    if (ACTION_GETCURRENTPOSITION.equals(action)) { 
     Log.i("Myactivity","inside getcurrentposition action"); 
     Location lastKnownLocation = locationManager.getLastKnownLocation(provider); 
     lastPosition = lastKnownLocation; 
     Log.i("Myactivity","last location "+lastKnownLocation); 
     if ((null != lastKnownLocation) && lastKnownLocation.getTime() + this.maximumAge > new Date().getTime()) { 
     Log.i("Myactivity","inside b4 gotLocation"); 
     gotLocation(lastKnownLocation); 
     } else { 
     ctx.runOnUiThread(new RunnableLocationListener(this, callbackId, locationManager, provider)); 
     } 
    } else { 
     error(new PluginResult(Status.INVALID_ACTION), callbackId); 
    } 
    return result; 
    } 

    public void gotLocation (Location location) { 
    Log.i("Myactivity","inside gotLocation"); 
    try { 
     Log.i("Myactivity","inside try"); 
     JSONObject geoposition = new JSONObject(); 
     JSONObject coords = new JSONObject(); 
     coords.put("latitude",   location.getLatitude()); 
     coords.put("longitude",  location.getLongitude()); 
     coords.put("altitude",   location.getAltitude()); 
     coords.put("accuracy",   location.getAccuracy()); 
     coords.put("altitudeAccuracy", null); 
     coords.put("heading",   null); 
     coords.put("speed",   location.getSpeed()); 
     geoposition.put("coords", coords); 
     geoposition.put("timestamp", location.getTime()); 
     geoposition.put("provider", location.getProvider()); 
     geoposition.put("lastPos", lastPosition); 
     success(new PluginResult(Status.OK, geoposition), callbackId); 
    } catch (JSONException jsonEx) { 
     error(new PluginResult(Status.JSON_EXCEPTION), callbackId); 
    } 
    } 

    protected String getBestProvider (LocationManager locationManager) { 
    String provider = LocationManager.PASSIVE_PROVIDER; 
    if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { 
     provider = LocationManager.GPS_PROVIDER; 
    } 
    else if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) { 
     provider = LocationManager.NETWORK_PROVIDER; 
    } 
    return provider; 
    } 

    class RunnableLocationListener extends Thread { 
    protected final NativeGeolocation plugin; 
    protected final LocationManager locationManager; 
    protected final String provider; 
    protected final String callbackId; 
    public Boolean ended = null; 
    public RunnableLocationListener (NativeGeolocation plugin, String callbackId, LocationManager locationManager, String provider) { 
     Log.i("Myactivity","inside runnabl"); 
     this.plugin = plugin; 
     this.locationManager = locationManager; 
     this.provider = provider; 
     this.callbackId = callbackId; 
    } 

    public void run() { 
     ended = false; 
     PluginResult result = null; 
     final LocationListener locationListener = new LocationListener() { 
     public void onLocationChanged (Location location) { 
      Log.i("Myactivity","calling getlocation again1"); 
      if (false == ended) { 
      Log.i("Myactivity","calling getlocation again2"); 
      plugin.gotLocation(location); 
      locationManager.removeUpdates(this); 
      } 
     } 
     public void onStatusChanged (String provider, int status, Bundle extras) { 
      Log.i("Myactivity","inside onStatus Changed"); 
    } 
     public void onProviderEnabled(String provider) { 
      Log.i("Myactivity","inside onProvider Enabled"); 
    } 
     public void onProviderDisabled(String provider) { 
      Log.i("Myactivity","inside onProvider Disabled"); 
    } 
     }; 
     locationManager.requestLocationUpdates(provider,1, 10, locationListener);//1 minutes and 10 meters 
     Thread timeouter = new Thread() { 
     public void run() { 
      try { 
      Thread.sleep(plugin.timeout); 
      ended = true; 
      locationManager.removeUpdates(locationListener); 
      plugin.error(new PluginResult(Status.ERROR, "timeout"), callbackId); 
      } catch (java.lang.InterruptedException ex) { 
      error(new PluginResult(Status.JSON_EXCEPTION), callbackId); 
      } 
     } 
     }; 
     timeouter.start(); 
    } 
    } 
} 

이것은 phonegap을 통해 전화를 걸고있는 자바 플러그인입니다. 하지만 인터넷을 사용할 수있는 경우에만 위치 정보를 제공합니다. 나는 gps 하드웨어를 사용하고 인터넷을 통해서만 위치 정보를 얻을 필요가있다. 어떤 도움 ??인터넷없이 위치 관리자가 작동하지 않습니다.

나는 이것과 관련된 stackoverflow 질문 중 일부를 언급했다. 그래서 GPS가 건물 내부에서 작동하지 않는다는 것을 알았습니다. 좋아,하지만 내 경우에는 외부에서도 작동하지 않는다. 마지막 위치가 알려지지 않을 때까지 작동하지 않습니다. 일단 그것이 마지막 위치에 있다면 그것은 어떻게 작동하는지 시작합니다. 어떤 도움 ??

편집 :이 링크 Android - Trouble in getting location coordinates by only using GPS provider에 따르면, 나는 libwlocate를 사용할 수 있습니다. 하지만 어떻게 그것을 phonegap 플러그인으로 사용 하시겠습니까 ?? 어떤 도움 ??

도움을 주시면 대단히 감사하겠습니다.

편집 : 처음으로 더미 임시 lastNumberLocation을 삽입하면 어떻습니까? 그렇다면 그것은 requestForNew 하나의 권리 것입니다 ?? 당신은 요청할 수 있습니다

+1

당신은 건물 내에서 언젠가 실 거예요 작업을 GPS 마음에 베어, 그래서 발코니 또는 뭔가 시도를 줄 필요가 ... 같이 "ONLY GPS". – Coderji

+0

인터넷이 꺼져있는 동안 어떤 종류의 예외 사항이 있는지 확인하십시오. – Techfist

+0

@Techfist 그래, 시간 초과 오류를 준다 – SSS

답변

1

LocationProvider gpsProvider = locationManager.getProvider(LocationManager.GPS_PROVIDER); 
if(gpsProvider != null){ 
    locationManager.requestLocationUpdates(gpsProvider.getName(), 0, 0, this); 
} 


Boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 
Location lastLocation = null, gpsLocation = null; 
if (isGPSEnabled) 
    gpsLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
관련 문제