2011-06-14 137 views
0

我可以通過網絡提供商獲取經度和緯度,但無法通過GPS獲取。我怎樣才能做到這一點?如何在Android中通過GPS獲取經度和緯度?

public void onCreate(Bundle savedInstanceState) { 
... 
mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
if (mLocation != null) { 

      gp1 = getGeoByLocation(mLocation); 
      gp2 = gp1; 

      refreshMapView(); 

      if(!mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){ 
       mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 2000, 
         10, mLocationListener); 
      }else{ 
       mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 
           10, mLocationListener); 
      } 

    private GeoPoint getGeoByLocation(Location location) { 
     GeoPoint gp = null; 
     try { 
      if (location != null) { 
       double geoLatitude = location.getLatitude() * 1E6; 
       double geoLongitude = location.getLongitude() * 1E6; 
       gp = new GeoPoint((int) geoLatitude, (int) geoLongitude); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return gp; 
    } 
+0

提供一些代碼工作,你已經執行到現在否則沒有人會幫助你。 – ManjotSingh 2011-06-14 11:48:19

+0

提供的代碼.. – 2011-06-14 13:19:48

回答

0

使用GPS每10分鐘獲得緯度。

// Des: Start Device's GPS and get current latitude and longitude 
    public void GPS() throws IOException { 

// Des: This is a background service and called after every 10 minutes and fetch     latitude-longitude values 
    background = new Thread(new Runnable() { 
    @Override 
    public void run() {    
     for (int i = 0; i < j; i++) { 
      if (ProjectStaticVariable.GPSExit == true) { 
      try { 
       Thread.sleep(600000); //10 minutes 
       mainhandler.sendMessage(mainhandler.obtainMessage()); 
       j++; 
      } 
      catch (Exception e) { 
       e.printStackTrace(); 
       throw new RuntimeException(e); 
      } 
      } 
     } 
    } 
}); 

background.start(); 
mainhandler = new Handler() { 
    public void handleMessage(Message msg) { 

// Check Internet status 
     isInternetPresent = cd.isConnectingToInternet(); 
     if (isInternetPresent) { 

      lat_long_Service_flag = true;     
      mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
      mlocListener = new MyLocationListener(getApplicationContext()); 
      mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 0, mlocListener); 

      mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

     } 
    } 
    }; 
} 

    // Des: Location Listener through which we get current latitude and longitude 
    public class MyLocationListener implements LocationListener { 

public MyLocationListener(Context mContext) {} 

public MyLocationListener(Runnable runnable) {} 

@Override 
public void onLocationChanged(Location loc) { 

    longitude = loc.getLongitude(); 
    latitude = loc.getLatitude(); 
    final_latitude = Double.toString(latitude); 
    final_longitude = Double.toString(longitude); 

} 

@Override 
public void onProviderDisabled(String provider) { 
} 

@Override 
public void onProviderEnabled(String provider) { 
} 

@Override 
public void onStatusChanged(String provider, int status, Bundle extras) { 
    } 
} 
相關問題