Guest User

Untitled

a guest
Feb 21st, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.23 KB | None | 0 0
  1. public class MainActivity extends AppCompatActivity {
  2.  
  3. MapView mMapView = null;
  4.  
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8.  
  9. Context ctx = getApplicationContext();
  10. Configuration.getInstance().load(ctx, PreferenceManager.getDefaultSharedPreferences(ctx));
  11.  
  12. setContentView(R.layout.activity_main);
  13.  
  14. mMapView = (MapView) findViewById(R.id.map);
  15. mMapView.setTileSource(TileSourceFactory.MAPNIK);
  16.  
  17. mMapView.setBuiltInZoomControls(true);
  18. mMapView.setMultiTouchControls(true);
  19. GeoPoint overlayCenterPoint = new GeoPoint(50.450667, 30.523193);
  20. IMapController mapController = mMapView.getController();
  21. mapController.setZoom(17f);
  22. mapController.setCenter(overlayCenterPoint);
  23. mMapView.setMapOrientation(0.0f);
  24.  
  25. GroundOverlay myGroundOverlay = new GroundOverlay();
  26. myGroundOverlay.setPosition(overlayCenterPoint);
  27. Drawable d = ResourcesCompat.getDrawable(getResources(), R.drawable.ic_launcher, null);
  28. myGroundOverlay.setImage(d.mutate());
  29. myGroundOverlay.setDimensions(200.0f);
  30. myGroundOverlay.setTransparency(0.25f);
  31. myGroundOverlay.setBearing(0);
  32. mMapView.getOverlays().add(myGroundOverlay);
  33.  
  34. mMapView.invalidate();
  35. }
  36.  
  37. @Override
  38. protected void onResume() {
  39. super.onResume();
  40. mMapView.onResume();
  41. }
  42.  
  43. @Override
  44. protected void onPause() {
  45. super.onPause();
  46. mMapView.onPause();
  47. }
  48.  
  49. public class GroundOverlay extends Overlay {
  50.  
  51. protected Drawable mImage;
  52. protected GeoPoint mPosition;
  53. protected float mBearing;
  54. protected float mWidth, mHeight;
  55. protected float mTransparency;
  56. public final static float NO_DIMENSION = -1.0f;
  57. protected Point mPositionPixels, mSouthEastPixels;
  58.  
  59. public GroundOverlay() {
  60. super();
  61. mWidth = 10.0f;
  62. mHeight = NO_DIMENSION;
  63. mBearing = 0.0f;
  64. mTransparency = 0.0f;
  65. mPositionPixels = new Point();
  66. mSouthEastPixels = new Point();
  67. }
  68.  
  69. public void setImage(Drawable image){
  70. mImage = image;
  71. }
  72.  
  73. public Drawable getImage(){
  74. return mImage;
  75. }
  76.  
  77. public GeoPoint getPosition(){
  78. return mPosition.clone();
  79. }
  80.  
  81. public void setPosition(GeoPoint position){
  82. mPosition = position.clone();
  83. }
  84.  
  85. public float getBearing(){
  86. return mBearing;
  87. }
  88.  
  89. public void setBearing(float bearing){
  90. mBearing = bearing;
  91. }
  92.  
  93. public void setDimensions(float width){
  94. mWidth = width;
  95. mHeight = NO_DIMENSION;
  96. }
  97.  
  98. public void setDimensions(float width, float height){
  99. mWidth = width;
  100. mHeight = height;
  101. }
  102.  
  103. public float getHeight(){
  104. return mHeight;
  105. }
  106.  
  107. public float getWidth(){
  108. return mWidth;
  109. }
  110.  
  111. public void setTransparency(float transparency){
  112. mTransparency = transparency;
  113. }
  114.  
  115. public float getTransparency(){
  116. return mTransparency;
  117. }
  118.  
  119. protected void computeHeight(){
  120. if (mHeight == NO_DIMENSION && mImage != null){
  121. mHeight = mWidth * mImage.getIntrinsicHeight() / mImage.getIntrinsicWidth();
  122. }
  123. }
  124.  
  125. /** @return the bounding box, ignoring the bearing of the GroundOverlay (similar to Google Maps API) */
  126. public BoundingBox getBoundingBox(){
  127. computeHeight();
  128. GeoPoint pEast = mPosition.destinationPoint(mWidth, 90.0f);
  129. GeoPoint pSouthEast = pEast.destinationPoint(mHeight, -180.0f);
  130. double north = mPosition.getLatitude()*2 - pSouthEast.getLatitude();
  131. double west = mPosition.getLongitude()*2 - pEast.getLongitude();
  132. return new BoundingBox(north, pEast.getLongitude(), pSouthEast.getLatitude(), west);
  133. }
  134.  
  135. public void setPositionFromBounds(BoundingBox bb){
  136. mPosition = bb.getCenterWithDateLine();
  137. GeoPoint pEast = new GeoPoint(mPosition.getLatitude(), bb.getLonEast());
  138. GeoPoint pWest = new GeoPoint(mPosition.getLatitude(), bb.getLonWest());
  139. mWidth = (float)pEast.distanceToAsDouble(pWest);
  140. GeoPoint pSouth = new GeoPoint(bb.getLatSouth(), mPosition.getLongitude());
  141. GeoPoint pNorth = new GeoPoint(bb.getLatNorth(), mPosition.getLongitude());
  142. mHeight = (float)pSouth.distanceToAsDouble(pNorth);
  143. }
  144.  
  145. @Override public void draw(Canvas canvas, MapView mapView, boolean shadow) {
  146. if (shadow)
  147. return;
  148. if (mImage == null)
  149. return;
  150.  
  151. computeHeight();
  152.  
  153. final Projection pj = mapView.getProjection();
  154.  
  155. pj.toPixels(mPosition, mPositionPixels);
  156. GeoPoint pEast = mPosition.destinationPoint(mWidth/2, 90.0f);
  157. GeoPoint pSouthEast = pEast.destinationPoint(mHeight/2, -180.0f);
  158. pj.toPixels(pSouthEast, mSouthEastPixels);
  159. int hWidth = mSouthEastPixels.x-mPositionPixels.x;
  160. int hHeight = mSouthEastPixels.y-mPositionPixels.y;
  161. mImage.setBounds(-hWidth, -hHeight, hWidth, hHeight);
  162.  
  163. mImage.setAlpha(255-(int)(mTransparency*255));
  164.  
  165. drawAt(canvas, mImage, mPositionPixels.x, mPositionPixels.y, false, -mBearing);
  166. }
  167.  
  168. }
  169. }
  170.  
  171. // overlay center point
  172. GeoPoint overlayCenterPoint = new GeoPoint(50.450667, 30.523193);
  173. IMapController mapController = mMapView.getController();
  174. mapController.setZoom(17f);
  175. mapController.setCenter(overlayCenterPoint);
  176. mMapView.setMapOrientation(0.0f);
  177.  
  178. GroundOverlay myGroundOverlay = new GroundOverlay();
  179. myGroundOverlay.setPosition(overlayCenterPoint);
  180. Drawable d = ResourcesCompat.getDrawable(getResources(), R.drawable.ic_launcher, null);
  181. myGroundOverlay.setImage(d.mutate());
  182.  
  183. // overlay width in meters (height calculated automatically) also you can set both width and height
  184. myGroundOverlay.setDimensions(200.0f);
  185. myGroundOverlay.setTransparency(0.25f);
  186. myGroundOverlay.setBearing(0);
  187. mMapView.getOverlays().add(myGroundOverlay);
Add Comment
Please, Sign In to add comment