Disable the autorun feature in the MyLocationOverlay component

I have a problem with the GoogleMap API for the Android component MyLocationOverlay. I want to disable the MyLocationOverlay autostart function (for the current location of the user) every time the location of the device changes (and the user's location goes beyond the visible part or map).

There seems to be no checkbox to disable the auto-center feature of unwanted cards when the user's location leaves the screen.

eg.

public class LocationTest extends MapActivity{ private MapView map; private MapController controller; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); map = (MapView) findViewById(R.id.map); final MyLocationOverlayUpdate overlay = new MyLocationOverlayUpdate(this, map); overlay.enableMyLocation(); map.getOverlays().add(overlay); } @Override protected boolean isRouteDisplayed() { return false; } } public class MyLocationOverlayUpdate extends MyLocationOverlay { public MyLocationOverlayUpdate(Context context, MapView mapView) { super(context, mapView); } public void drawMyLocation(Canvas canvas, MapView mapView, Location location, GeoPoint geoPoint, long when) {} } 

I scanned the network for this problem, but could not find a solution.

Additional information found:

  • I assume the documentation is erroneous, erroneous, or simply outdated, saying:

    Additionally, the designer can also take the MapController and use it to save the β€œmy location” point, visible by panning the map when it enters the screen

because the constructor does not accept MapController as an argument, and it seems that I have no choice over keeping the "my location" dot visible or not.

  • I accidentally found a workaround by overriding the drawMyLocation () method without calling super.drawMyLocation () solves the problem of re-placing the map (the unwanted function is "disabled"). However, I need to override the drawMyLocation () method, so change the default value (and spend time ...)

Maybe there is a clearer solution?

+4
source share
2 answers

extend the MyLocationOverlay class and override the overlay animation by overriding the drawMyLocation method.

 public class CustomMyLocationOverlay extends MyLocationOverlay { private static final String TAG = "Bankomatai"; private Drawable[] slips; private final MapView mapView; private int currSlip = 0; private Location savedFix = null; Point point = new Point(); Rect rect = new Rect(); private Handler handler = new Handler(); private Runnable overlayAnimationTask; public CustomMyLocationOverlay(Context context, MapView mapView) { super(context, mapView); this.mapView = mapView; slips = new Drawable[4]; slips[0] = context.getResources().getDrawable(R.drawable.ic_maps_indicator_current_position_1); slips[1] = context.getResources().getDrawable(R.drawable.ic_maps_indicator_current_position_2); slips[2] = context.getResources().getDrawable(R.drawable.ic_maps_indicator_current_position_3); slips[3] = context.getResources().getDrawable(R.drawable.ic_maps_indicator_current_position_4); overlayAnimationTask = new Runnable() { @Override public void run() { currSlip = (currSlip + 1) % slips.length; CustomMyLocationOverlay.this.mapView.invalidate(); handler.removeCallbacks(overlayAnimationTask); handler.postDelayed(overlayAnimationTask, 200); } }; handler.removeCallbacks(overlayAnimationTask); handler.postAtTime(overlayAnimationTask, 100); } protected void drawMyLocation(Canvas canvas, MapView mapView, Location lastFix, GeoPoint myLocation, long when) { Log.v(TAG, "draw: " + System.currentTimeMillis()); mapView.getProjection().toPixels(myLocation, point); rect.left = point.x - slips[currSlip].getIntrinsicWidth() / 2; rect.top = point.y - slips[currSlip].getIntrinsicHeight() / 2; rect.right = point.x + slips[currSlip].getIntrinsicWidth() / 2; rect.bottom = point.y + slips[currSlip].getIntrinsicHeight() / 2; slips[currSlip].setBounds(rect); slips[currSlip].draw(canvas); } } 

plus you need to create 4 icons and put them in the res / drawable folder under the names ic_maps_indicator_current_position_1, ic_maps_indicator_current_position_2, ic_maps_indicator_current_position_3, ic_maps_indicator_current_position_4. You need to draw them yourself.

0
source
 public class CustomMyLocationOverlay extends MyLocationOverlay { @Override protected void drawMyLocation(Canvas canvas, MapView mapView, Location location, GeoPoint geoPoint, long when) { if (!recenter) { mapView.getController().stopAnimation(false); } // Now draw the location overlay. super.drawMyLocation(canvas, mapView, location, geoPoint, when); } } 

When changing a location, the map reviewer uses the same logic as the map animation, so by stopping the animation, I was able to stop re-centering the map. Hope this helps you. It worked for me.

0
source

Source: https://habr.com/ru/post/1389346/


All Articles