Android emulator Android开发-GPS不工作-发送坐标时应用程序挂起
问题背景: 我对安卓开发有些陌生。目前,我所要做的就是从手机的GPS设备上获取GPS坐标,并将其显示在谷歌地图上,或者显示在Android emulator Android开发-GPS不工作-发送坐标时应用程序挂起,android-emulator,gps,Android Emulator,Gps,问题背景: 我对安卓开发有些陌生。目前,我所要做的就是从手机的GPS设备上获取GPS坐标,并将其显示在谷歌地图上,或者显示在文本视图中,或者其他任何地方。主要任务是得到它们 我读过很多教程。我正在使用 -安卓2.3.3 -API等级10 -Eclipse 3.6.1 -视窗7 问题描述: 我已经编写了一个类GeoUpdateHandler,它实现了LocationListener并实现了它的方法onLocationChange()等等。在MyMapsActivity中,我使用LocationM
文本视图中,或者其他任何地方。主要任务是得到它们
我读过很多教程。我正在使用
-安卓2.3.3
-API等级10
-Eclipse 3.6.1
-视窗7
问题描述:
我已经编写了一个类GeoUpdateHandler
,它实现了LocationListener
并实现了它的方法onLocationChange()
等等。在MyMapsActivity
中,我使用LocationManager
的定期RequestLocationUpdate
来获得定期更新
运行应用程序时,我从Eclipse的Emulator控件(窗口-->显示视图-->其他-->Android-->Emulator控件)发送GPS坐标以发送经度和纬度。一旦仿真设备获得坐标,应用程序就会挂起。什么也没发生,光标变为蓝色圆圈(表示思考/卡住),什么也没发生。如果我使用locationManager.getLastKnownLocation(字符串提供程序)
,它将变为null。显然,因为没有最后的已知位置。它在认识一个之前就被卡住了
我也尝试过通过telnet发送坐标。(cmd-->telnet localhost 5554-->地理修复。但同样的情况也会发生
我已经尝试独立于应用程序启动设备,发送坐标,然后启动应用程序。但运行应用程序时也会发生同样的情况:它挂起
以下代码HelloItemizeOverlay
取自Android的MapView教程,可用于手动给定的坐标。当尝试检索GPS位置时,会出现问题
public class MyMapsActivity extends MapActivity
{
private MapController mapController;
private MapView mapView;
private LocationManager locationManager;
LinearLayout linLayout;
MapView mView;
List<Overlay> mapOverlays;
Drawable drawable;
HelloItemizedOverlay itemizedOverlay;
GeoUpdateHandler handler;
Location location;
public void onCreate(Bundle bundle)
{
super.onCreate(bundle);
setContentView(R.layout.main); // bind the layout to the activity
mView = (MapView) findViewById(R.id.mapview);
mView.setBuiltInZoomControls(true);
mapOverlays = mView.getOverlays();
drawable = this.getResources().getDrawable(R.drawable.icon);
itemizedOverlay = new HelloItemizedOverlay(drawable);
mapController = mView.getController();
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
handler = new GeoUpdateHandler();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 0, handler);
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
GeoPoint point = new GeoPoint((int)location.getLongitude(),(int)location.getLatitude());
OverlayItem overlayitem = new OverlayItem(point, "", "");
mView.setBuiltInZoomControls(true);
itemizedOverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedOverlay);
mapController.setZoom(8);
mapController.animateTo(point);
}
@Override
protected boolean isRouteDisplayed()
{
return false;
}
public class GeoUpdateHandler implements LocationListener
{
@Override
public void onLocationChanged(Location location)
{
int lat = (int) (location.getLatitude());
int lng = (int) (location.getLongitude());
GeoPoint point = new GeoPoint(lat, lng);
mapController.animateTo(point); // mapController.setCenter(point);
}
@Override
public void onProviderDisabled(String provider)
{
}
@Override
public void onProviderEnabled(String provider)
{
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
}
@Override
protected void onResume()
{
handler = new GeoUpdateHandler();
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 0, handler);
super.onResume();
}
}
公共类MyMapsActivity扩展了MapActivity
{
专用地图控制器;
私有地图视图;
私人场所经理场所经理;
线性布局;
地图视图;
列出地图覆盖图;
可拉伸;
HelloItemizedOverlay itemizedOverlay;
GeoUpdateHandler处理器;
位置;
创建公共void(Bundle)
{
super.onCreate(bundle);
setContentView(R.layout.main);//将布局绑定到活动
mView=(MapView)findViewById(R.id.MapView);
mView.setBuiltInZoomControls(真);
mapOverlays=mView.getOverlays();
drawable=this.getResources().getDrawable(R.drawable.icon);
itemizedOverlay=新HelloItemizedOverlay(可绘制);
mapController=mView.getController();
locationManager=(locationManager)getSystemService(Context.LOCATION\u服务);
handler=新的GeoUpdateHandler();
locationManager.RequestLocationUpdate(locationManager.GPS_提供程序,3000,0,处理程序);
location=locationManager.getLastKnownLocation(locationManager.GPS\U提供程序);
地理点=新的地理点((int)location.getLongitude(),(int)location.getlation());
OverlayItem OverlayItem=新的OverlayItem(点“,”);
mView.setBuiltInZoomControls(真);
itemizedOverlay.addOverlay(overlayitem);
添加(itemizedOverlay);
mapController.setZoom(8);
mapController.animateTo(点);
}
@凌驾
受保护的布尔值isRouteDisplayed()
{
返回false;
}
公共类GeoUpdateHandler实现LocationListener
{
@凌驾
已更改位置上的公共无效(位置)
{
intlat=(int)(location.getLatitude());
int lng=(int)(location.getLongitude());
地质点=新的地质点(纬度、液化天然气);
mapController.animateTo(点);//mapController.setCenter(点);
}
@凌驾
公共无效onProviderDisabled(字符串提供程序)
{
}
@凌驾
公共无效onProviderEnabled(字符串提供程序)
{
}
@凌驾
public void onStatusChanged(字符串提供程序、int状态、Bundle extra)
{
}
}
@凌驾
受保护的void onResume()
{
handler=新的GeoUpdateHandler();
locationManager=(locationManager)getSystemService(Context.LOCATION\u服务);
locationManager.RequestLocationUpdate(locationManager.GPS_提供程序,5000,0,处理程序);
super.onResume();
}
}
根据,地质点
构造函数将(纬度*1e6,经度*1e6)作为参数;而您在调用getLastKnownLocation
时输入(经度,纬度),而在另一个调用中忘记了比例因子(在onLocationChanged
中)。纬度和逻辑度在对象中不进行缩放
无论如何,此错误只会导致显示蓝色地图(0,0位于大西洋),因此可能存在其他问题
编辑:
1/在重播您的评论时,如果您混淆了经度和纬度,您可能会得到一个不存在的点(纬度始终保持在[-90°90°范围内,而经度可以在[-180°180°范围内变化)
2/SDK 2.3(API级别9)中有一个错误,它使模拟器在发送模拟位置时崩溃。不知道2.3.3(lvl 10)是否正确,但我使用2.1u1(lvl 7)进行测试
3/以下代码适用于我:
package test.testmap;
import java.util.List;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
public class MyMapsActivity extends MapActivity
{
private MapController mapController;
private LocationManager locationManager;
LinearLayout linLayout;
MapView mView;
List<Overlay> mapOverlays;
Drawable drawable;
HelloItemizedOverlay itemizedOverlay;
GeoUpdateHandler handler;
Location location;
public void onCreate(Bundle bundle)
{
super.onCreate(bundle);
setContentView(R.layout.main); // bind the layout to the activity
drawable = this.getResources().getDrawable(R.drawable.icon);
itemizedOverlay = new HelloItemizedOverlay(drawable);
mView = (MapView) findViewById(R.id.mapview);
mView.setBuiltInZoomControls(true);
mapOverlays = mView.getOverlays();
mapOverlays.add(itemizedOverlay);
mapController = mView.getController();
}
@Override
protected boolean isRouteDisplayed()
{
return false;
}
public class GeoUpdateHandler implements LocationListener
{
@Override
public void onLocationChanged(Location location)
{
Log.d(this.getClass().getName(),"onLocationChanged : lat = "+location.getLatitude()+" lon = "+location.getLongitude());
int lat = (int) Math.round(location.getLatitude()*1.0e6);
int lng = (int) Math.round(location.getLongitude()*1.0e6);
GeoPoint point = new GeoPoint(lat, lng);
itemizedOverlay.addOverlay(new OverlayItem(point, "", ""));
mapController.animateTo(point);
mapController.setZoom(8);
}
@Override
public void onProviderDisabled(String provider)
{
Log.d(this.getClass().getName(),"onProviderDisabled");
}
@Override
public void onProviderEnabled(String provider)
{
Log.d(this.getClass().getName(),"onProviderEnabled");
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
Log.d(this.getClass().getName(),"onStatusChanged");
}
}
@Override
protected void onResume()
{
handler = new GeoUpdateHandler();
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 0, handler);
super.onResume();
}
}
package test.testmap;
导入java.util.List;
导入android.content.Context;
导入android.graphics.drawable.drawable;
导入android.location.location;
导入android.location.LocationListener;
导入android.location.LocationManager;
导入android.os.Bundle;
导入android.util.Log;
导入android.widget.LinearLayout;
导入com.google.android.maps.GeoPoint;
导入com.google.android.maps.MapActivity;
导入com.google.android.maps.MapController;
导入com.google.android.maps.MapView;
导入com.google.android.maps.Overlay;
导入com.google.android.maps.OverlayItem;
公共类MyMapsActivity扩展了MapActivity
{
专用地图控制器;
私人位置经理loc