Turn the marker over the heading device to the exact location when the map rotates.

Using the newer mapsforge 0.5.1 the map rotates fine with the onSensorChanged event. I added a custom GPS location marker at a GPS point to the onLocationChanged event. When the map is rotated, the marker is also rotated to point to the device title, just like in google maps using the following code in the marker drawing method.

android.graphics.Canvas androidCanvas = AndroidGraphicFactory.getCanvas(canvas);
androidCanvas.save();
Float px = (float) canvas.getWidth()/2;
Float py = (float) canvas.getHeight()/2;
androidCanvas.rotate(degree, px, py);
canvas.drawBitmap(bitmap, left, top);
androidCanvas.restore();

      

enter image description here

However, when the map is dragged, the marker position is disoriented from the previous GPS location point.

enter image description here

How to calculate the px and py value so that the marker is at the exact location of the GPS location on the map even when the map rotates, and also the marker pointing to the device title.

+3


source to share


1 answer


I know this is a little out of date, but for future research, I found a solution:

  • First pass the Markforge Marker and override its draw () method
  • Copy the content of the draw method from Marker
  • then add the following in the last condition:

Replace this:

if(canvasRectangle.intersects(bitmapRectangle)) {
    canvas.drawBitmap(bitmap, left, top);
}

      



Wherein:

if(canvasRectangle.intersects(bitmapRectangle)) {
    // Rotate the marker based on bearing for example (or whatever angle you want)
    android.graphics.Canvas androidCanvas = AndroidGraphicFactory.getCanvas(canvas);
    androidCanvas.save();

    float px = (right + left) / 2f;
    float py = (bottom + top) / 2f;

    androidCanvas.rotate(bearing, px, py);
    canvas.drawBitmap(bitmap, left, top);
    androidCanvas.restore();
}

      

In my case "bearing" is a member variable of my class extending a marker that I set from two different locations earlier in the code (gps location). you can replace it with any angle that suits your needs.

+1


source







All Articles