2012-04-13 69 views
0

嗨,哥們兒我是android新手我在使用LocationManager.GPS_PROVIDER創建位置對象時得到空值。如何在android中獲取位置對象

這裏我GPSDemoActivity:

import android.app.Activity; 
import android.content.Context; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Toast; 

public class GPSDemoActivity extends Activity { 

    private static final long MINIMUM_DISTANCE = 1; 
    private static final long MINIMUM_TIME = 1000; 

    protected LocationManager locationManager; 

    // private Button showlocationButton; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     // showlocationButton = (Button) findViewById(R.id.btn_showlocation); 
     locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 
       MINIMUM_DISTANCE, MINIMUM_TIME, new MyLocationListener()); 
    } 

    public void showLocation(View v) { 
     Toast.makeText(this, "Clicked", Toast.LENGTH_LONG).show(); 
     Location location = locationManager 
       .getLastKnownLocation(LocationManager.GPS_PROVIDER);//here getting null. 

     if (location != null) { 
      String message = String.format(
        "Current Location: \n Longitude: %1$s \n Latitude: %2$s", 
        location.getLongitude(), location.getLatitude()); 
      Toast.makeText(this, message, Toast.LENGTH_LONG).show(); 
     } 

    } 

    private class MyLocationListener implements LocationListener { 

     @Override 
     public void onLocationChanged(Location location) { 
      String message = String.format(
        "Your Location \n Longitude: %1$s \n Latitude: %2$s", 
        location.getLongitude(), location.getLatitude()); 
      Toast.makeText(GPSDemoActivity.this, message, Toast.LENGTH_LONG); 
     } 

     @Override 
     public void onProviderDisabled(String provider) { 

      Toast.makeText(GPSDemoActivity.this, "Provider disabled by the user. GPS turned off", Toast.LENGTH_LONG).show(); 

     } 

     @Override 
     public void onProviderEnabled(String provider) { 

      Toast.makeText(GPSDemoActivity.this, 
        "Provider disabled by the user. GPS turned on", 
        Toast.LENGTH_LONG).show(); 

     } 

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

      Toast.makeText(GPSDemoActivity.this, 
        "Provider status changed", Toast.LENGTH_LONG).show(); 


     } 

    } 
} 

這裏我下面的XML。 main.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <Button 
     android:id="@+id/btn_showlocation" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:text="@string/btn_showlocation" 
     android:layout_gravity="center" 
     android:onClick="showLocation" 
     /> 

</LinearLayout> 

這裏是我的manifest.xml。 我的manifest.xml:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.gps" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk android:minSdkVersion="8" /> 

    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" > 
     <activity 
      android:name=".GPSDemoActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 


    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 

</manifest> 
+0

請發佈您的logcat錯誤。 – atluriajith 2012-04-13 09:47:29

回答

0

locationManager.getLastKnownLocation返回只有當你能夠檢測位置ATLEAST一次有效值。

是使用GPS設備上啓用確定位置搜索,只有位置經理可以retreieve位置

+0

嗨naresh謝謝你的迴應我啓用了GPS在我的設備,它的工作如果我使用「LocationManager.NETWORK_PROVIDER」而不是「LocationManager.GPS_PROVIDER」 – 2012-04-13 09:50:45

+0

是這些是獲取位置的兩種方式,只有當您有清晰的線路時,視線與天空。如果你是indores,可能需要20分鐘才能獲得位置。 – Naresh 2012-04-13 09:51:57

+0

ohh k naresh我明白非常感謝你....... – 2012-04-13 09:57:27

0

@Bhaskar - 測試應用了側的建設作爲GPS將不能在建築物內獲得的位置。你的代碼沒問題。