首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在android中获取Location对象

如何在android中获取Location对象
EN

Stack Overflow用户
提问于 2012-04-13 17:41:37
回答 2查看 1.9K关注 0票数 0

嗨,伙计们,我是安卓新手,我在使用LocationManager.GPS_PROVIDER创建location对象时遇到了null。

这是我的GPSDemoActivity:

代码语言:javascript
复制
 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:

代码语言:javascript
复制
 <?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:

代码语言:javascript
复制
 <?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>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-04-13 17:45:47

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

您是否确定在您的设备上启用了使用gps的位置搜索,只有这样位置管理器才能检索位置

票数 0
EN

Stack Overflow用户

发布于 2012-04-13 17:56:20

@Bhaskar --在建筑物外测试应用程序,因为gps无法获得建筑物内的位置。你的代码没问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10138553

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档