2013-08-26 37 views
0

我陷入了一個問題,以獲得在Android設備中的經度和長度。 看看我的代碼如下: -在Android設備中獲取位置的問題

public NewLocationActivity(final Context mContext) { 

       locationManager = (LocationManager) mContext 
       .getSystemService(Context.LOCATION_SERVICE); 

       locationListener = new LocationListener() { 

      public void onLocationChanged(Location location) { 

      updateLocation(location); 



       mSharedPreferences=mContext.getSharedPreferences(HomeSAFEPref.HomeSAFELocationPref,Context.MODE_PRIVATE); 
       SharedPreferences.Editor prefEditor=mSharedPreferences.edit(); 

       String latitude=String.valueOf(currentLatitude); 
       String longitude=String.valueOf(currentLongitude); 
       String heading=String.valueOf(currentheading); 
       String horizAccuracy=String.valueOf(currenthorizAccuracy); 

       prefEditor.putString("Latitude", latitude); 
       prefEditor.putString("Longitude", longitude); 
       prefEditor.putString("Heading", heading); 
       prefEditor.putString("HorizAccuracy", horizAccuracy); 
       prefEditor.commit(); 

       } 

      public void onStatusChanged(String provider, int status, 
        Bundle extras) { 
      } 

      public void onProviderEnabled(String provider) { 

      } 

      public void onProviderDisabled(String provider) { 

      } 

     }; 

     locationManager.requestLocationUpdates(
       LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); 
    } 

    void updateLocation(Location location) { 
    currentLocation = location; 
    this. currentLatitude = currentLocation.getLatitude(); 
    this. currentLongitude = currentLocation.getLongitude(); 

    geoField=new GeomagneticField(
      Double.valueOf(location.getLatitude()).floatValue(), 
      Double.valueOf(location.getLongitude()).floatValue(), 
      Double.valueOf(location.getAltitude()).floatValue(), 
      System.currentTimeMillis() 
     ); 

    this.currentheading=geoField.getInclination(); 
    this.currenthorizAccuracy=currentLocation.getAccuracy(); 
    this.speed=(int) currentLocation.getSpeed(); 
} 

當我創建類NewLocationActivity構造的對象的工作。但我的問題是,更新位置方法不runs.Why我不知道......但有些時候在其他設備它運行完美,我得到lat和long easily.Please讓我知道爲什麼我得到經緯度和長0,0.Thanks提前..

回答

1

使用此代碼:它得到拉特,長從你的internet.u也可以使用gps獲取lat,long。它是準確的,但不會在建築物內工作,或者可能在某些設備上,因爲它們的接收能力。最好的方法是使用兩個並寫入條件,如果gps lat,長爲空去爲w IFI

public class NewLocationActivity extends Activity implements LocationListener { 


    private LocationManager locationManager; 

    /** 
    * Called when the activity is first created. 
    */ 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.digitalsignature); 
      getLocation(); 

    } 


    public String[] getLocation() 
    { 

     String[] locationArray=new String[2]; 


     locationManager = (LocationManager) DigitalSignatureActivity.this.getSystemService(LOCATION_SERVICE); 
     // getting network status 
     boolean isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 

     // The minimum distance to change Updates in meters 
     final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters 

     // The minimum time between updates in milliseconds 
     final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute 

     if (isNetworkEnabled) { 
      locationManager.requestLocationUpdates(
        LocationManager.NETWORK_PROVIDER, 
        MIN_TIME_BW_UPDATES, 
        MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
      Log.d("Network", "Network"); 
      if (locationManager != null) { 
       Location location = locationManager 
         .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
       if (location != null) { 
        locationArray[0] = String.valueOf(location.getLatitude()); 
        locationArray[1] = String.valueOf(location.getLongitude()); 

       } 
      } 
     } 
     return locationArray; 

    } 


    /** 
    * Stop using location listener 
    * Calling this function will stop using location updates in your app 
    * */ 
    public void stopUsingLocationUpdates(){ 
     if(locationManager != null){ 
      locationManager.removeUpdates(DigitalSignatureActivity.this); 
     }  
    } 


    @Override 
    public void onLocationChanged(Location arg0) { 
    // TODO Auto-generated method stub 

    } 

    @Override 
    public void onProviderDisabled(String arg0) { 
    // TODO Auto-generated method stub 

    } 

    @Override 
     public void onProviderEnabled(String arg0) { 
    // TODO Auto-generated method stub 

    } 

    @Override 
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) { 
    // TODO Auto-generated method stub 

    } 



} 

也表現宣告這樣

<uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
+0

http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/ – Ruban