简体   繁体   English

模拟器上的Android位置

[英]Android location on emulator

I'm trying this code for android location, but when I run the emulator it says "unfortunately location has stopped"... any idea? 我正在尝试将此代码用于android位置,但是当我运行模拟器时,它说“不幸的是位置已停止” ...知道吗? This the code for MainAcivity (I followed this tutorial http://www.vogella.com/articles/AndroidLocationAPI/article.html#locationapi_gpsenable ) 这是MainAcivity的代码(我遵循了本教程http://www.vogella.com/articles/AndroidLocationAPI/article.html#locationapi_gpsenable

    public class MainActivity extends Activity implements LocationListener {
      private TextView latituteField;
      private TextView longitudeField;
      private LocationManager locationManager;
      private String provider;

/** Called when the activity is first created. */

      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        latituteField = (TextView) findViewById(R.id.TextView02);
        longitudeField = (TextView) findViewById(R.id.TextView04);

// Get the location manager
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Define the criteria how to select the locatioin provider -> use
// default
        Criteria criteria = new Criteria();
        provider = locationManager.getBestProvider(criteria, false);
        Location location = locationManager.getLastKnownLocation(provider);

// Initialize the location fields
        if (location != null) {
          System.out.println("Provider " + provider + " has been selected.");
          onLocationChanged(location);
        } else {
          latituteField.setText("Location not available");
          longitudeField.setText("Location not available");
        }
      }

/* Request updates at startup */
      @Override
      protected void onResume() {
        super.onResume();
        locationManager.requestLocationUpdates(provider, 400, 1, this);
      }

/* Remove the locationlistener updates when Activity is paused */
      @Override
      protected void onPause() {
        super.onPause();
        locationManager.removeUpdates(this);
      }

      @Override
      public void onLocationChanged(Location location) {
        int lat = (int) (location.getLatitude());
        int lng = (int) (location.getLongitude());
        latituteField.setText(String.valueOf(lat));
        longitudeField.setText(String.valueOf(lng));
      }

      @Override
      public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

      }

      @Override
      public void onProviderEnabled(String provider) {
        Toast.makeText(this, "Enabled new provider " + provider,
            Toast.LENGTH_SHORT).show();

      }

      @Override
      public void onProviderDisabled(String provider) {
        Toast.makeText(this, "Disabled provider " + provider,
            Toast.LENGTH_SHORT).show();
      }
    }   

you have to enter the location of your emulator manually. 您必须手动输入仿真器的位置。 There are several options 有几种选择

Option 1: using DDMS 选项1:使用DDMS

  • go to your android/tools directory, and launch the DDMS tool 转到您的android / tools目录,然后启动DDMS工具

  • select the Emulator Control Tab 选择仿真器控制选项卡

  • fill the Location Controls fields with the longitude and latitude values 用经度和纬度值填充位置控制字段

  • press the Send button 按发送按钮

Option 2: using telnet 选项2:使用telnet

  • open a command shell 打开命令外壳
  • conect to the emulator with the command: telnet localhost 使用以下命令连接到仿真器:telnet localhost
  • to enter a fixed location execute the command: 要输入固定位置,请执行以下命令:

geo fix < longitude> < latitude> [< altitude>] 地理定位<经度> <纬度> [<海拔>]

Option 3: using 3rd party app 选项3:使用第三方应用程序

You can try android-gps-emulator app to set the location. 您可以尝试使用android-gps-emulator应用设置位置。 There might be other apps like this 可能还有其他类似的应用

Source 资源

Try this code i may be help to you.. 试试这个代码,我可能会为您提供帮助。

http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/ http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/

Also Setting the geoposition You can use the "DDMS" Perspective of Eclipse to send your geoposition to the emulator or a connected device. 还可以设置地理区域您可以使用“ DDMS” Eclipse透视图将地理区域发送到仿真器或连接的设备。 For open this Perspective select Window → Open Perspective → Other... → DDMS. 要打开此透视图,请选择窗口→打开透视图→其他...→DDMS。

In the Emulator Control part you can enter the geocoordinates and press the Send button. 在“仿真器控制”部分,您可以输入地理坐标,然后按“发送”按钮。

在此处输入图片说明

您可以使用Genymotion模拟器,在虚拟设备上设置位置非常容易。

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

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