【问题标题】:Sending location to emulator向模拟器发送位置
【发布时间】:2012-05-18 17:17:01
【问题描述】:

我正在测试使用内置 Eclipse 工具将坐标发送到 Android 模拟器的选项。但它似乎没有收到它们。我有这个简单的代码:

public class Main extends MapActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    MapView view = (MapView) findViewById(R.id.themap);
    view.setBuiltInZoomControls(true);

    final MapController mp = view.getController();

    LocationManager manager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

    LocationListener listener = new LocationListener() {

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

        }

        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

        }

        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub

        }

        public void onLocationChanged(final Location location) {
            // TODO Auto-generated method stub
            mp.setCenter(new GeoPoint((int)location.getLatitude(), (int)location.getLongitude()));

        }
    };
    manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);

}



@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}
}

我在 Manifest 中拥有 ACCESS_FINE_LOCATION 和 INTERNET 的权限。 如果有人有任何解释,我将不胜感激。

【问题讨论】:

  • 我不想给出这个笼统的答案,但是,如果它不起作用,那么你在模拟器方面做错了。它可能不在您的代码中。
  • 还有哪些可能的错误?
  • 不幸的是,我能给你的最好建议是准确研究应该如何完成,并找出你偏离它的地方。此外,谷歌和 SO 搜索将提供大量信息。

标签: android google-maps android-emulator


【解决方案1】:

你添加了吗

<uses-library android:name="com.google.android.maps" />

到清单中的应用程序标记? 您还需要 Google 提供的 SDK 调试证书的 MD5 指纹才能接收 Google 地图数据。你可以得到它here

尝试在 onLocationChanged 方法中放置一个断点,看看它是否在向模拟器发送位置命令时停止。当您还没有证书时,这也应该有效。

【讨论】:

  • 它似乎并没有停止在 onLocationChanged...知道为什么吗??
  • 您是否为模拟器添加了 gps 支持?转到 avd 管理器,然后单击 avd 上的编辑。然后在“硬件”部分添加 gps 支持条目。当您将位置数据发送到模拟器时,您应该会在通知栏中看到 gps 图标。
  • 我看到了那个图标,但位置没有改变:(
【解决方案2】:

这对我有用。

监听器实现...

public class MyLocationListener implements LocationListener {
    public static final String TAG = "MyLocationListener";

    private Context context;

    public MyLocationListener(Context c) {
        this.context = c;
    }

public void onLocationChanged(android.location.Location location) {
        Log.d(TAG,"LocationChanged: Lat: "+location.getLatitude()+" Lng: "+location.getLongitude());
    }
}

用法...

MyLocationListener locationListener = new MyLocationListener(this);
// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,locationListener);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-15
    • 1970-01-01
    • 1970-01-01
    • 2018-03-05
    • 2012-05-21
    • 1970-01-01
    • 2012-05-03
    • 2018-06-29
    相关资源
    最近更新 更多