简体   繁体   English

在Android模拟器上设置GPS位置

[英]Setting GPS position on android emulator

I am trying to set a GPS position on my emulator with telnet and using DDMS perspective. 我正在尝试使用telnet并使用DDMS透视图在模拟器上设置GPS位置。 Nothing seems to be working, because if you think my code the output on the screen will be "Couldn't get a GPS location". 似乎没有任何效果,因为如果您认为我的代码在屏幕上显示为“无法获取GPS位置”。 The application works when I run it on a phone. 当我在手机上运行时,该应用程序可以正常工作。 Code: 码:

@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>

From the DDMS Perspective from the Emulator Control tab located in the bottom-left of Eclipse you have Locations Controls . 从位于Eclipse左下角的Emulator Control选项卡的DDMS Perspective ,您可以看到Locations Controls From there you can input the longitude and latitude and click Send to send those coordinates to the selected emulator. 从那里您可以输入经度和纬度,然后单击发送以将这些坐标发送到选定的模拟器。

Maybe you will still not be able to get the last know location, but this will fire the onLocationChanged() method. 也许你仍然无法获得最后知道的位置,但这将触发onLocationChanged()方法。

EDIT: I see that your class implements LocationListener . 编辑:我看到您的类实现了LocationListener Did you register your class for the location changes using lm.requestLocationUpdates() ? 您是否使用lm.requestLocationUpdates()注册了您的课程以更改位置?

EDIT2: I mean : 编辑2:我的意思是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM