在Android模拟器上设置GPS位置

时间:2011-11-29 12:15:33

标签: android gps emulation

我正在尝试使用telnet并使用DDMS透视图在我的模拟器上设置GPS位置。似乎没有什么工作,因为如果你认为我的代码屏幕上的输出将是“无法获得GPS位置”。当我在手机上运行时,该应用程序可以正常工作。代码:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    tvLat = (TextView) findViewById(R.id.tvLat);
    tvLongi = (TextView) findViewById(R.id.tvLongi);
    tvLat.setText("test");
    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Criteria crit = new Criteria();
    providers = lm.getBestProvider(crit, false);
    Location loc = lm.getLastKnownLocation(providers);
    if (loc != null) {
        x =  loc.getLatitude();
        y =  loc.getLongitude();

        t = "Current latitude: " + x;
        t1 = "Current longitude: " + y;
        tvLat.setText(t);
        tvLongi.setText(t1);
    } else {
        tvLat.setText("Couldn't get a GPS location");
    }

}

@Override
public void onLocationChanged(Location loc) {
    // TODO Auto-generated method stub
    x = loc.getLatitude();
    y = loc.getLongitude();
    t = "Current latitude: " + x;
    t1 = "Current longitude: " + y;

    tvLat.setText(t);
    tvLongi.setText(t1);

}

@Override
public void onProviderDisabled(String arg0) {
    // TODO Auto-generated method stub
    tvLat.setText("GPS disabled");
}

@Override
public void onProviderEnabled(String arg0) {

    tvLat.setText("GPS enabled");
}

@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {


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

1 个答案:

答案 0 :(得分:3)

来自Eclipse左下角的DDMS Perspective标签中的Emulator Control,您有Locations Controls。从那里你可以输入经度和纬度,然后单击发送将这些坐标发送到选定的模拟器。

也许您仍然无法获取最后知道的位置,但这会触发onLocationChanged()方法。

编辑:我看到你的班级实现了LocationListener。您是否使用lm.requestLocationUpdates()

在课程中注册了地点更改?

EDIT2:我的意思是:

lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,this);
lm.requestLocationUpdates(LocationManager.GPS, 0, 0,this);