在Android中模拟器上检索上一个位置?

时间:2013-01-09 12:23:12

标签: android gps eclipse-adt

我正在尝试获取最后一个位置,但它总是失败,这是我的代码:

activity_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" >
    <TextView
        android:id="@+id/tv1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="hello" />
</LinearLayout>


MainActivity.java

package com.example.gps;

import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {

    LocationManager mLocationManager;
    TextView tv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView) findViewById(R.id.tv1);
        mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        criteria.setPowerRequirement(Criteria.POWER_LOW);
        String locationprovider = mLocationManager.getBestProvider(criteria, true);
        Location mLocation = mLocationManager.getLastKnownLocation(locationprovider);
        tv.setText("Last location lat:" + mLocation.getLatitude() + " long:" + mLocation.getLongitude());
    }
}


的AndroidManifest.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"
        android:targetSdkVersion="17" />

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.gps.MainActivity"
            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>

</manifest>


我通过DDMS /仿真器控制窗口发送坐标,如下图所示: &

正如您在AVD详细信息(hw.gps)中看到的那样,为我的AVD启用了GPS硬件: enter image description here

您可能会看到在模拟器中启用了位置访问权限: enter image description here

我还尝试使用命令通过telnet发送坐标:

telnet localhost 5556 
geo fix 13.24 52.31 


但遗憾的是,getLastKnownLocation()方法始终返回null! 如何解决这个问题?

4 个答案:

答案 0 :(得分:0)

像这样更改你的代码

if(locationprovider!=null)
{
 Location mLocation = mLocationManager.getLastKnownLocation(locationprovider);
}
else
{
locationprovider =LocationManager.GPS_PROVIDER;
 Location mLocation = mLocationManager.getLastKnownLocation(locationprovider);
}

答案 1 :(得分:0)

对于那些感兴趣的人,我发现了问题&amp;解决方案
这是因为模拟器之前从未检索过某个位置,因此最后一个位置始终为空! 为避免这种情况,我们必须使用以下代码至少获取一次位置:

public class MainActivity extends Activity implements LocationListener {
    LocationManager mLocationManager;
    TextView tv;
    Location mLocation;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView) findViewById(R.id.tv1);
        mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        criteria.setPowerRequirement(Criteria.POWER_LOW);
        String locationprovider = mLocationManager.getBestProvider(criteria, true);
        mLocationManager.requestLocationUpdates(locationprovider, 5000, (float) 2.0, this);
    }

    @Override
    public void onLocationChanged(Location location) {
        mLocation = location;
        showupdate();
    }

    public void onProviderDisabled(String arg0) {
    }

    public void onProviderEnabled(String provider) {
    }

    public void onStatusChanged(String a, int b, Bundle c) {
    }

    public void showupdate() {
        tv.setText("Last location lat:" + mLocation.getLatitude() + " long:" + mLocation.getLongitude());
    }
}

然后,无论何时运行问题中发布的初始代码,它都将正确执行:)

答案 2 :(得分:0)

在模拟器中你不会对GPS有任何支持..如果我们通过坐标它会很简单。就是这样。在模拟器中我们可以刺激假gps位置来测试它。我们不能指望那个最后的位置..

答案 3 :(得分:0)

我猜你错过了互联网权限:

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

似乎您正在使用Android的默认模拟器,但您应该首先使用Google API并为Geopoint设置一些坐标。