Java 在Emulator上工作,但不在真正的Android设备上工作
我制作了一个Android应用程序,在Java 在Emulator上工作,但不在真正的Android设备上工作,java,android,gps,android-mapview,Java,Android,Gps,Android Mapview,我制作了一个Android应用程序,在Android屏幕的上半部分显示谷歌地图,在Android屏幕的下半部分显示文本视图 在我的谷歌地图(安卓屏幕的上半部分)上,我在我的当前位置上画了一个圆圈,我在模拟器中从DDMS透视图传递我的当前位置(纬度和长距离值)。它在模拟器中运行良好 当我在模拟器上启动一个应用程序时,首先它在android屏幕的上半部分显示一个Google地图,在下半部分显示一个TextView。然后使用DDMS透视图,我正在传递我的当前位置(lat和long值),传递后,它在谷歌
Android屏幕的上半部分显示谷歌地图
,在Android屏幕的下半部分显示文本视图
在我的谷歌地图(安卓屏幕的上半部分)
上,我在我的当前位置上画了一个圆圈
,我在模拟器中从DDMS透视图传递我的当前位置(纬度和长距离值)
。它在模拟器中运行良好
当我在模拟器上启动一个应用程序时,首先它在android屏幕的上半部分显示一个Google地图,在下半部分显示一个TextView。然后使用DDMS透视图,我正在传递我的当前位置(lat和long值),传递后,它在谷歌地图上的当前位置上画一个圆圈
在模拟器中一切都很好
问题陈述:-
但是当我在真正的Android手机上尝试同样的东西时,只有我的googlemaps
正确地显示在Android屏幕的上半部分,而TextView显示在Android屏幕的下半部分。但它并没有在我当前的位置上画出圆圈
我在Android手机上连接了wi-fi。我不这么认为,我需要在Android手机上传递纬度和经度值,对吗?它应该自动获取当前位置,对吗
或者在我的Android手机上我需要做什么
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener = new GPSLocationListener();
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
0,
0,
locationListener);
mapView = (MapView) findViewById(R.id.mapView);
listView = (ListView) findViewById(R.id.mylist);
mapView.setStreetView(true);
mapView.setBuiltInZoomControls(true);
mapController = mapView.getController();
mapController.setZoom(15);
}
private class GPSLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
if (location != null) {
GeoPoint point = new GeoPoint(
(int) (location.getLatitude() * 1E6),
(int) (location.getLongitude() * 1E6));
findUsersInCurrentRadius(4,location.getLatitude(),location.getLongitude());
mapController.animateTo(point);
mapController.setZoom(15);
// add marker
MapOverlay mapOverlay = new MapOverlay(this,android.R.drawable.star_on,R.drawable.tenm,R.drawable.twentym,R.drawable.thirtym,R.drawable.fourtym);
mapOverlay.setPointToDraw(point);
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
String address = ConvertPointToLocation(point);
Toast.makeText(getBaseContext(), address, Toast.LENGTH_SHORT).show();
mapView.invalidate();
}
}
class MapOverlay extends Overlay {
private GeoPoint pointToDraw;
int[] imageNames=new int[6];
public MapOverlay(GPSLocationListener gpsLocationListener,
int currentUser, int tenm, int twentym, int thirtym, int fourtym) {
imageNames[0]=currentUser;
imageNames[1]=tenm;
imageNames[2]=twentym;
imageNames[3]=thirtym;
imageNames[4]=fourtym;
}
public void setPointToDraw(GeoPoint point) {
pointToDraw = point;
}
public GeoPoint getPointToDraw() {
return pointToDraw;
}
@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
super.draw(canvas, mapView, shadow);
//---translate the GeoPoint to screen pixels---
Point screenPts = new Point();
mapView.getProjection().toPixels(pointToDraw, screenPts);
//--------------draw circle----------------------
Point pt = mapView.getProjection().toPixels(pointToDraw,screenPts);
Paint circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
circlePaint.setColor(0x30000000);
circlePaint.setStyle(Style.FILL_AND_STROKE);
int totalCircle=4;
int radius=40;
int centerimagesize=35;
for (int i = 1; i <= totalCircle; i ++) {
canvas.drawCircle(screenPts.x,screenPts.y, i*radius, circlePaint);
}
canvas.drawBitmap(BitmapFactory.decodeResource(getResources(),imageNames[0]), (screenPts.x-(centerimagesize/2)),(screenPts.y-(centerimagesize/2)), null);
super.draw(canvas,mapView,shadow);
return true;
}
}
@覆盖
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationManager=(locationManager)getSystemService(Context.LOCATION\u服务);
locationListener=新的GPSLocationListener();
locationManager.RequestLocationUpdate(
LocationManager.GPS\u提供程序,
0,
0,
位置(监听器);
mapView=(mapView)findViewById(R.id.mapView);
listView=(listView)findViewById(R.id.mylist);
mapView.setStreetView(真);
mapView.SetBuilTinZoomControl(真);
mapController=mapView.getController();
mapController.setZoom(15);
}
私有类GPSLocationListener实现LocationListener{
@凌驾
已更改位置上的公共无效(位置){
如果(位置!=null){
地质点=新的地质点(
(int)(location.getLatitude()*1E6),
(int)(location.getLongitude()*1E6));
findUsersInCurrentRadius(4,location.getLatitude(),location.getLongitude());
mapController.animateTo(点);
mapController.setZoom(15);
//添加标记
MapOverlay MapOverlay=新的MapOverlay(这个,android.R.drawable.star_on,R.drawable.tenm,R.drawable.twentym,R.drawable.thirtym,R.drawable.fourtym);
mapOverlay.setPointToDraw(点);
List ListoForLays=mapView.getOverlays();
listOfOverlays.clear();
添加(映射覆盖);
字符串地址=转换点位置(点);
Toast.makeText(getBaseContext(),地址,Toast.LENGTH_SHORT).show();
mapView.invalidate();
}
}
类MapOverlay扩展了Overlay{
私人地质点绘制;
int[]imageNames=新的int[6];
公共地图覆盖(GPSLocationListener GPSLocationListener,
int currentUser、int tenm、int twentym、int thirtym、int fourtym){
imageNames[0]=当前用户;
imageNames[1]=tenm;
imageNames[2]=二十米;
imageNames[3]=thirtym;
imageNames[4]=fourtym;
}
公共无效设置点ToDraw(地质点){
pointToDraw=点;
}
公共地质点getPointToDraw(){
返回点ToDraw;
}
@凌驾
公共布尔绘制(画布画布、地图视图、地图视图、布尔阴影、长时间){
super.draw(画布、地图视图、阴影);
//---将地质点转换为屏幕像素---
点屏幕PTS=新点();
mapView.getProjection().toPixels(pointToDraw、screenPts);
//--------------画圈----------------------
Point pt=mapView.getProjection().toPixels(pointToDraw,screenPts);
油漆回路油漆=新油漆(油漆.防油漆别名标志);
circlePaint.setColor(0x30000000);
循环绘制设置样式(样式填充和笔划);
int totalCircle=4;
int半径=40;
int centerimagesize=35;
对于(inti=1;i您又缺少一个GPS权限,请将此权限添加到您的AndroidManifest.xml中
<uses-permission android:name="android.permission.ACCESS_GPS"></uses-permission>
我可以从您的代码中看出,只有在收到第一个修复后才绘制圆。
您是否在手机设置(位置和安全)中启用了GPS?如果没有,请启用它
只有在获得GPS的第一次定位后,您才能获得圆。您需要将GPS带到室外才能获得圆
在Location Listener中有一个Toast,所以,如果您得到的Toast值为address,上述任何一项都不能解决您的问题
最后,为了提高效率,我会将创建和添加覆盖的代码移出Location Listener,因为每次您收到新的修复程序时,创建和删除覆盖都会有一定的难度。我已经尝试过将其添加到我的AndroidManifest中,但它仍然不会在我的Android手机上的当前位置上画圆圈。还有什么我不知道的吗唱
<uses-permission android:name="android.permission.ACCESS_GPS"></uses-permission>