如何在android中获取Location对象

时间:2012-04-13 09:41:37

标签: android

嗨,伙计我是android新手我在使用LocationManager.GPS_PROVIDER创建位置对象时得到null。

这里我的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>

2 个答案:

答案 0 :(得分:0)

只有当您能够至少检测到一次位置时,locationManager.getLastKnownLocation才会返回有效值。

您确定在您的设备上启用了使用gps进行位置搜索,只有位置管理员才能恢复位置

答案 1 :(得分:0)

@Bhaskar - 测试应用程序在你的建筑物旁边,因为gps不会获得建筑物内的位置。你的代码没问题就没问题了。