目前,每35秒更改一次位置并移动10米的距离后Circle
,我就会Current Location
在上绘制一个。
因此,我LocationChanged
在位置更改(移动35秒和10米)后立即实现了此功能,我正在绘制Circle on the Google Maps on the Current Location
。
问题陈述:-
My App is running very slow
。有时我的应用程序被挂起?可能是因为我的代码在下面的编写方式中效率不高?
基本上,我只需要在每移动35秒和10米的距离后在当前位置绘制一个圆圈。
我的代码有问题吗?任何想法,例如我应该如何进一步改善它,以使其顺利运行。
@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,
35000,
10,
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);
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();
}
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
这是我在MapOverlay类中绘制的圆。
class MapOverlay extends Overlay {
private GeoPoint pointToDraw;
int[] imageNames=new int[6];
public MapOverlay(GPSLocationListener gpsLocationListener, int currentUser) {
imageNames[0]=currentUser;
}
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;
}
}
更新代码:
private MapView mapView;
private ListView listView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mapView = (MapView) findViewById(R.id.mapView);
listView = (ListView) findViewById(R.id.mylist);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener = new GPSLocationListener(mapView);
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
35000,
0,
locationListener);
mapView.setStreetView(true);
mapView.setBuiltInZoomControls(true);
mapController = mapView.getController();
mapController.setZoom(15);
}
private class GPSLocationListener implements LocationListener {
MapOverlay mapOverlay;
public GPSLocationListener(MapView mapView) {
mapOverlay = new MapOverlay(this,android.R.drawable.star_on);
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.add(mapOverlay);
}
@Override
public void onLocationChanged(Location location) {
if (location != null) {
GeoPoint point = new GeoPoint(
(int) (location.getLatitude() * 1E6),
(int) (location.getLongitude() * 1E6));
mapController.animateTo(point);
mapController.setZoom(15);
// **See no need to make a new Object here**
mapOverlay.setPointToDraw(point);
mapView.invalidate();
}
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
class MapOverlay extends Overlay {
private GeoPoint pointToDraw;
int[] imageNames=new int[6];
private Point mScreenPoints;
private Bitmap mBitmap;
private Paint mCirclePaint;
public MapOverlay(GPSLocationListener gpsLocationListener, int currentUser) {
imageNames[0]=currentUser;
mCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mCirclePaint.setColor(0x30000000);
mCirclePaint.setStyle(Style.FILL_AND_STROKE);
mBitmap = BitmapFactory.decodeResource(getResources(),imageNames[0]);
mScreenPoints = new Point();
}
public void setPointToDraw(GeoPoint point) {
pointToDraw = point;
}
public GeoPoint getPointToDraw() {
return pointToDraw;
}
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
super.draw(canvas, mapView, shadow);
mScreenPoints = mapView.getProjection().toPixels(pointToDraw, mScreenPoints);
int totalCircle=4;
int radius=40;
int centerimagesize=35;
for (int i = 1; i <= totalCircle; i ++) {
canvas.drawCircle(mScreenPoints.x,mScreenPoints.y, i*radius, mCirclePaint);
}
canvas.drawBitmap(mBitmap, (mScreenPoints.x-(centerimagesize/2)),(mScreenPoints.y-(centerimagesize/2)), null);
super.draw(canvas,mapView,shadow);
return true;
}
}
绘制时需要记住以下几点:
可能的UI线程阻塞
这段代码看起来像在回调上调用一个潜在的昂贵动作onLocationChanged()
,这样做真的很危险,因为您可能会遇到ANR。这可能应该在后台AsyncTask上完成,然后在其结果上显示吐司。
String address = ConvertPointToLocation(point);
Toast.makeText(getBaseContext(), address, Toast.LENGTH_SHORT).show();
更好地管理地图资源
确保每次都回收实例并重置其位置,而不是每次都添加新的叠加层。
private class GPSLocationListener implements LocationListener {
MapOverlay mOverlay;
public GPSLocationListener() {
}
@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);
if (mOverlay == null) {
// Add this marker to the list of overlays always.
// This stuff never changes so there is no need to do this logic
// Every 30 secs. Loading images is **Expensive**
mOverlay = mMapOverlay = new MapOverlay(this,android.R.drawable.star_on);
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.add(mMapOverlay);
}
// **See, no need to make a new Object here**
mOverlay.setPointToDraw(point);
// This can probably be done at another time.
// String address = ConvertPointToLocation(point);
// Toast.makeText(getBaseContext(), address, Toast.LENGTH_SHORT).show();
mapView.invalidate();
}
这段代码可以重用此标记,并且只需更新其位置即可。如果不在列表中,则只应创建一个。
更好的绘图
好的,接下来请记住,如果不需要,不要在onDraw()方法中创建对象。一旦标记知道要绘制到的位置,就应该缓存所有内容,以便您可以专注于绘制。例如:
public class MapOverlay {
private GeoPoint pointToDraw;
int[] imageNames=new int[6];
// This is the cached Point on the screen that will get refilled on every draw
private Point mScreenPoints;
// This is the cached decoded bitmap that will be drawn each time
private Bitmap mBitmap;
// Cached Paint
private Paint mCirclePaint;
public MapOverlay(GPSLocationListener gpsLocationListener, int currentUser) {
imageNames[0]=currentUser;
// This only needs to be made here, once. It never needs to change.
mCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mCirclePaint.setColor(0x30000000);
mCirclePaint.setStyle(Style.FILL_AND_STROKE);
// We only need to load this image once and then just keep drawing it when dirtyed.
mBitmap = BitmapFactory.decodeResource(context.getResources(),imageNames[0]);
// This Point object will be changed every call to toPixels(), but the instance can be recycled
mScreenPoints = new Point();
}
public void setPointToDraw(GeoPoint point) {
pointToDraw = point;
}
public GeoPoint getPointToDraw() {
return pointToDraw;
}
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
super.draw(canvas, mapView, shadow);
// In the case where nothing has been set yet, don't do any drawing
if (pointToDraw == null) {
return true;
}
//--------------draw circle----------------------
mScreenPoints = mapView.getProjection().toPixels(pointToDraw, mScreenPoints);
int totalCircle=4;
int radius=40;
int centerimagesize=35;
for (int i = 1; i <= totalCircle; i ++) {
canvas.drawCircle(screenPts.x,screenPts.y, i*radius, mCirclePaint);
}
canvas.drawBitmap(mBitmap, (screenPts.x-(centerimagesize/2)),(screenPts.y-(centerimagesize/2)), null);
super.draw(canvas,mapView,shadow);
return true;
}
我想知道什么是电池效率最高的方式发送准确的位置更新到服务器/防火墙每5秒,即使应用程序关闭或手机重新启动。我尝试使用alarmmanager.setrepeating和android.intent.action.boot_completed接收器- 使用post一个延迟消息或runnable到一个处理程序不是一个可靠的解决方案,因为一旦应用程序被删除,位置更新就会停止。 是否有可靠的方法每5秒向服
当我滚动时,我正在使用。当您滚动到底部时,它将更新数据。更新数据后。返回顶部。理想情况下,我希望保留中的职位。我该怎么做呢? InnerClass
问题内容: 我正在学习如何使用新的Swift语言(只有Swift,没有Objective-C)。为此,我想用地图()做一个简单的视图。我想查找并更新用户的位置(例如在Apple Map应用程序中)。 我试过了,但是什么也没发生: 请你帮助我好吗? 问题答案: 您必须重写(CLLocationManagerDelegate的一部分)才能在位置管理器检索当前位置时得到通知: 注意:如果目标是iOS 8
谁能给我建议一种简单快速的方法,只需一次就能得到位置?!
问题内容: 我有以下Python Tkinter代码,该代码每10秒重绘一次标签。我的问题是,对我而言,似乎是一遍又一遍地在旧标签上绘制新标签。因此,最终,几个小时后,将有数百个图纸重叠(至少从我的理解来看)。这会占用更多内存还是会引起问题? 在我的示例中,我仅使用一个标签。我知道可以用来更新标签甚至是文本。但是实际要做的是刷新标签(如表格),按钮和填充物的网格,以与可用的最新数据相匹配。 从我了
我一直在尝试创建一个可重用的页脚组件,并希望它放置在底部,而不考虑页眉和页脚之间的内容,但对于某些页面,它会留在底部,而某些页面,它要么在顶部,要么在顶部之间。 有人能帮我一下吗? app.component.html app.component.css 页脚.组件 footer.component.css