PageRenderTime 51ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 1ms

/android/Dummy/OSM2/src/main/java/org/andnav/osm/views/OpenStreetMapView.java

https://gitlab.com/baijifeilong/dummy
Java | 2177 lines | 1094 code | 248 blank | 835 comment | 111 complexity | cdc90847bd0b685cab8fed914ec55b2e MD5 | raw file
Possible License(s): GPL-2.0
  1. // Created by plusminus on 17:45:56 - 25.09.2008
  2. package org.andnav.osm.views;
  3. /*import gt.software.maps.DrawTuchArmy;
  4. import gt.software.maps.MainMapActivity;
  5. import gt.software.maps.R;
  6. import gt.software.maps.TipTSBH;
  7. import gt.software.maps.overlays.DrawOverlay;
  8. import gt.software.maps.overlays.QBOverlay;
  9. import gt.software.net.InfoDetailUI;
  10. import gt.software.net.InfoImageUI;
  11. import gt.software.net.InfoResourceAdapter;
  12. import gt.software.net.InfoVideoUI;*/
  13. import java.io.File;
  14. import java.io.FileInputStream;
  15. import java.io.FileNotFoundException;
  16. import java.io.IOException;
  17. import java.io.InputStream;
  18. import java.util.ArrayList;
  19. import java.util.HashMap;
  20. import java.util.List;
  21. import java.util.Set;
  22. import javax.xml.parsers.DocumentBuilder;
  23. import javax.xml.parsers.DocumentBuilderFactory;
  24. import javax.xml.parsers.ParserConfigurationException;
  25. import org.andnav.osm.util.BoundingBoxE6;
  26. import org.andnav.osm.util.GeoPoint;
  27. import org.andnav.osm.util.MyMath;
  28. import org.andnav.osm.util.constants.GeoConstants;
  29. import org.andnav.osm.util.constants.OpenStreetMapConstants;
  30. import org.andnav.osm.views.controller.OpenStreetMapViewController;
  31. import org.andnav.osm.views.overlay.OpenStreetMapViewOverlay;
  32. import org.andnav.osm.views.util.OpenStreetMapRendererInfo;
  33. import org.andnav.osm.views.util.OpenStreetMapTileDownloader;
  34. import org.andnav.osm.views.util.OpenStreetMapTileFilesystemProvider;
  35. import org.andnav.osm.views.util.OpenStreetMapTileProvider;
  36. import org.andnav.osm.views.util.Util;
  37. import org.andnav.osm.views.util.VersionedGestureDetector;
  38. import org.andnav.osm.views.util.constants.OpenStreetMapViewConstants;
  39. import org.w3c.dom.Document;
  40. import org.w3c.dom.Element;
  41. import org.w3c.dom.Node;
  42. import org.w3c.dom.NodeList;
  43. import org.xml.sax.SAXException;
  44. import android.app.Dialog;
  45. import android.content.Context;
  46. import android.content.Intent;
  47. import android.content.res.Configuration;
  48. import android.database.Cursor;
  49. import android.graphics.Bitmap;
  50. import android.graphics.Canvas;
  51. import android.graphics.Color;
  52. import android.graphics.Paint;
  53. import android.graphics.Paint.Style;
  54. import android.graphics.Path;
  55. import android.graphics.Point;
  56. import android.graphics.Rect;
  57. import android.os.Bundle;
  58. import android.os.Environment;
  59. import android.os.Handler;
  60. import android.os.Message;
  61. import android.util.AttributeSet;
  62. import android.util.Log;
  63. import android.view.GestureDetector;
  64. import android.view.GestureDetector.OnDoubleTapListener;
  65. import android.view.GestureDetector.OnGestureListener;
  66. import android.view.KeyEvent;
  67. import android.view.MotionEvent;
  68. import android.view.View;
  69. import android.widget.Button;
  70. import android.widget.LinearLayout;
  71. import android.widget.RelativeLayout.LayoutParams;
  72. import android.widget.ScrollView;
  73. import android.widget.TextView;
  74. import android.widget.Toast;
  75. public class OpenStreetMapView extends View implements OpenStreetMapConstants,
  76. OpenStreetMapViewConstants {
  77. // ===========================================================
  78. // Constants
  79. // ===========================================================
  80. // ===========================================================
  81. // Fields
  82. // ===========================================================
  83. //从mapActivity里面提取出来的flag
  84. private boolean mapflag=false;
  85. private SimpleInvalidationHandler mSimpleInvalidationHandler;
  86. protected int mLatitudeE6 = 0, mLongitudeE6 = 0;
  87. protected int mZoomLevel = 0;
  88. public float mBearing = 0;
  89. private boolean mActionMoveDetected;
  90. private boolean mStopMoveDetecting;
  91. protected OpenStreetMapRendererInfo mRendererInfo;
  92. protected final OpenStreetMapTileProvider mTileProvider;
  93. protected final GestureDetector mGestureDetector = new GestureDetector(
  94. new OpenStreetMapViewGestureDetectorListener());
  95. private VersionedGestureDetector mDetector = VersionedGestureDetector
  96. .newInstance(new GestureCallback());
  97. protected final List<OpenStreetMapViewOverlay> mOverlays = new ArrayList<OpenStreetMapViewOverlay>();
  98. protected final Paint mPaint = new Paint();
  99. protected final Paint mfPaint = new Paint();
  100. public int mTouchDownX;
  101. public int mTouchDownY;
  102. public int mTouchMapOffsetX;
  103. public int mTouchMapOffsetY;
  104. public double mTouchScale;
  105. private double mTouchDiagonalSize;
  106. private OpenStreetMapView mMiniMap, mMaxiMap;
  107. private OpenStreetMapViewController mController;
  108. private int mMiniMapOverriddenVisibility = NOT_SET;
  109. private int mMiniMapZoomDiff = NOT_SET;
  110. private Handler mMainActivityCallbackHandler;
  111. private ArrayList<Point> pointlist = null;// added by cui 2013 3 19
  112. // added by cui
  113. private Context context;
  114. private double d_lon = 0;
  115. private double d_lat = 0;
  116. double last_max_lon;
  117. double last_min_lon;
  118. double last_max_lat;
  119. double last_min_lat;
  120. public boolean clickable = true;
  121. // end
  122. // ===========================================================
  123. // Constructors
  124. // ===========================================================
  125. /**
  126. * Standard Constructor for {@link OpenStreetMapView}.
  127. *
  128. * @param context
  129. * @param aRendererInfo
  130. * pass a {@link OpenStreetMapRendererInfo} you like.
  131. */
  132. public OpenStreetMapView(final Context context,
  133. OpenStreetMapRendererInfo aRendererInfo) {
  134. super(context);
  135. this.context = context;
  136. this.mSimpleInvalidationHandler = new SimpleInvalidationHandler();
  137. this.mRendererInfo = aRendererInfo;
  138. this.mTileProvider = new OpenStreetMapTileProvider(context,
  139. mSimpleInvalidationHandler, aRendererInfo,
  140. CACHE_MAPTILECOUNT_DEFAULT);
  141. this.mPaint.setAntiAlias(true);
  142. this.mTouchScale = 1;
  143. mfPaint.setColor(Color.RED);
  144. mfPaint.setStyle(Style.STROKE);
  145. mfPaint.setStrokeWidth(2);
  146. setFocusable(true);
  147. setFocusableInTouchMode(true);
  148. }
  149. public OpenStreetMapView(Context context, AttributeSet attrs) {
  150. super(context, attrs);
  151. this.context = context;
  152. this.mSimpleInvalidationHandler = new SimpleInvalidationHandler();
  153. this.mTileProvider = new OpenStreetMapTileProvider(context,
  154. mSimpleInvalidationHandler, new OpenStreetMapRendererInfo(getResources(), ""),
  155. CACHE_MAPTILECOUNT_DEFAULT);
  156. this.mPaint.setAntiAlias(true);
  157. this.mTouchScale = 1;
  158. mfPaint.setColor(Color.RED);
  159. mfPaint.setStyle(Style.STROKE);
  160. mfPaint.setStrokeWidth(2);
  161. setFocusable(true);
  162. setFocusableInTouchMode(true);
  163. }
  164. public Handler getHandler() {
  165. return mSimpleInvalidationHandler;
  166. }
  167. /**
  168. *
  169. * @param context
  170. * @param aRendererInfo
  171. * pass a {@link OpenStreetMapRendererInfo} you like.
  172. * @param osmv
  173. * another {@link OpenStreetMapView}, to share the TileProvider
  174. * with.<br/>
  175. * May significantly improve the render speed, when using the
  176. * same {@link OpenStreetMapRendererInfo}.
  177. */
  178. // public OpenStreetMapView(final Context context, final
  179. // OpenStreetMapRendererInfo aRendererInfo,
  180. // final OpenStreetMapView aMapToShareTheTileProviderWith) {
  181. // super(context);
  182. // this.mRendererInfo = aRendererInfo;
  183. // this.mTileProvider = aMapToShareTheTileProviderWith.mTileProvider;
  184. // this.mPaint.setAntiAlias(true);
  185. // }
  186. // ===========================================================
  187. // Getter & Setter
  188. // ===========================================================
  189. /**
  190. * This MapView takes control of the {@link OpenStreetMapView} passed as
  191. * parameter.<br />
  192. * I.e. it zoomes it to x levels less than itself and centers it the same
  193. * coords.<br />
  194. * Its pretty usefull when the MiniMap uses the same TileProvider.
  195. *
  196. * @see OpenStreetMapView.OpenStreetMapView(
  197. * @param aOsmvMinimap
  198. * @param aZoomDiff
  199. * 3 is a good Value. Pass {@link OpenStreetMapViewConstants}
  200. * .NOT_SET to disable autozooming of the minimap.
  201. */
  202. public void setMiniMap(final OpenStreetMapView aOsmvMinimap,
  203. final int aZoomDiff) {
  204. this.mMiniMapZoomDiff = aZoomDiff;
  205. this.mMiniMap = aOsmvMinimap;
  206. aOsmvMinimap.setMaxiMap(this);
  207. // Synchronize the Views.
  208. this.setMapCenter(this.mLatitudeE6, this.mLongitudeE6);
  209. this.setZoomLevel(this.getZoomLevel());
  210. }
  211. public boolean hasMiniMap() {
  212. return this.mMiniMap != null;
  213. }
  214. /**
  215. * @return {@link View}.GONE or {@link View}.VISIBLE or {@link View}
  216. * .INVISIBLE or {@link OpenStreetMapViewConstants}.NOT_SET
  217. * */
  218. public int getOverrideMiniMapVisiblity() {
  219. return this.mMiniMapOverriddenVisibility;
  220. }
  221. /**
  222. * Use this method if you want to make the MiniMap visible i.e.: always or
  223. * never. Use {@link View}.GONE , {@link View}.VISIBLE, {@link View}
  224. * .INVISIBLE. Use {@link OpenStreetMapViewConstants}.NOT_SET to reset this
  225. * feature.
  226. *
  227. * @param aVisiblity
  228. */
  229. public void setOverrideMiniMapVisiblity(final int aVisiblity) {
  230. switch (aVisiblity) {
  231. case View.GONE:
  232. case View.VISIBLE:
  233. case View.INVISIBLE:
  234. if (this.mMiniMap != null)
  235. this.mMiniMap.setVisibility(aVisiblity);
  236. case NOT_SET:
  237. this.setZoomLevel(this.mZoomLevel);
  238. break;
  239. default:
  240. throw new IllegalArgumentException(
  241. "See javadoch of this method !!!");
  242. }
  243. this.mMiniMapOverriddenVisibility = aVisiblity;
  244. }
  245. protected void setMaxiMap(final OpenStreetMapView aOsmvMaxiMap) {
  246. this.mMaxiMap = aOsmvMaxiMap;
  247. }
  248. public OpenStreetMapViewController getController() {
  249. if (this.mController != null)
  250. return this.mController;
  251. else
  252. return this.mController = new OpenStreetMapViewController(this);
  253. }
  254. /**
  255. * You can add/remove/reorder your Overlays using the List of
  256. * {@link OpenStreetMapViewOverlay}. The first (index 0) Overlay gets drawn
  257. * first, the one with the highest as the last one.
  258. */
  259. public List<OpenStreetMapViewOverlay> getOverlays() {
  260. return this.mOverlays;
  261. }
  262. public double getLatitudeSpan() {
  263. return this.getDrawnBoundingBoxE6().getLongitudeSpanE6() / 1E6;
  264. }
  265. public int getLatitudeSpanE6() {
  266. return this.getDrawnBoundingBoxE6().getLatitudeSpanE6();
  267. }
  268. public double getLongitudeSpan() {
  269. return this.getDrawnBoundingBoxE6().getLatitudeSpanE6() / 1E6;
  270. }
  271. public int getLongitudeSpanE6() {
  272. return this.getDrawnBoundingBoxE6().getLatitudeSpanE6();
  273. }
  274. public BoundingBoxE6 getDrawnBoundingBoxE6() {
  275. return getBoundingBox(this.getWidth(), this.getHeight());
  276. }
  277. public BoundingBoxE6 getVisibleBoundingBoxE6() {
  278. // final ViewParent parent = this.getParent();
  279. // if(parent instanceof RotateView){
  280. // final RotateView par = (RotateView)parent;
  281. // return getBoundingBox(par.getMeasuredWidth(),
  282. // par.getMeasuredHeight());
  283. // }else{
  284. return getBoundingBox(this.getWidth(), this.getHeight());
  285. // }
  286. }
  287. private BoundingBoxE6 getBoundingBox(final int pViewWidth,
  288. final int pViewHeight) {
  289. /*
  290. * Get the center MapTile which is above this.mLatitudeE6 and
  291. * this.mLongitudeE6 .
  292. */
  293. final int[] centerMapTileCoords = Util.getMapTileFromCoordinates(
  294. this.mLatitudeE6, this.mLongitudeE6, this.mZoomLevel, null,
  295. this.mRendererInfo.PROJECTION);
  296. final BoundingBoxE6 tmp = Util.getBoundingBoxFromMapTile(
  297. centerMapTileCoords, this.mZoomLevel, mRendererInfo.PROJECTION);
  298. final int mLatitudeSpan_2 = (int) (1.0f * tmp.getLatitudeSpanE6()
  299. * pViewHeight / this.mRendererInfo
  300. .getTileSizePx(this.mZoomLevel)) / 2;
  301. final int mLongitudeSpan_2 = (int) (1.0f * tmp.getLongitudeSpanE6()
  302. * pViewWidth / this.mRendererInfo
  303. .getTileSizePx(this.mZoomLevel)) / 2;
  304. final int north = this.mLatitudeE6 + mLatitudeSpan_2;
  305. final int south = this.mLatitudeE6 - mLatitudeSpan_2;
  306. final int west = this.mLongitudeE6 - mLongitudeSpan_2;
  307. final int east = this.mLongitudeE6 + mLongitudeSpan_2;
  308. return new BoundingBoxE6(north, east, south, west);
  309. }
  310. /**
  311. * This class is only meant to be used during on call of onDraw(). Otherwise
  312. * it may produce strange results.
  313. *
  314. * @return
  315. */
  316. public OpenStreetMapViewProjection getProjection() {
  317. return new OpenStreetMapViewProjection();
  318. }
  319. public void setMapCenter(final GeoPoint aCenter) {
  320. this.setMapCenter(aCenter.getLatitudeE6(), aCenter.getLongitudeE6());
  321. }
  322. public void setMapCenter(final double aLatitude, final double aLongitude) {
  323. this.setMapCenter((int) (aLatitude * 1E6), (int) (aLongitude * 1E6));
  324. }
  325. public void setMapCenter(final int aLatitudeE6, final int aLongitudeE6) {
  326. this.setMapCenter(aLatitudeE6, aLongitudeE6, true);
  327. }
  328. protected void setMapCenter(final int aLatitudeE6, final int aLongitudeE6,
  329. final boolean doPassFurther) {
  330. this.mLatitudeE6 = aLatitudeE6;
  331. this.mLongitudeE6 = aLongitudeE6;
  332. if (doPassFurther && this.mMiniMap != null)
  333. this.mMiniMap.setMapCenter(aLatitudeE6, aLongitudeE6, false);
  334. else if (this.mMaxiMap != null)
  335. this.mMaxiMap.setMapCenter(aLatitudeE6, aLongitudeE6, false);
  336. this.postInvalidate();
  337. }
  338. public void setBearing(final float aBearing) {
  339. this.mBearing = aBearing;
  340. }
  341. public float getBearing() {
  342. return this.mBearing;
  343. }
  344. public boolean setRenderer(final OpenStreetMapRendererInfo aRenderer) {
  345. this.mRendererInfo = aRenderer;
  346. final boolean ret = this.mTileProvider.setRender(aRenderer,
  347. mSimpleInvalidationHandler);
  348. Log.i("zoomlever", this.mZoomLevel + ".." + aRenderer.ZOOM_MAXLEVEL
  349. + "-" + aRenderer.ZOOM_MINLEVEL);
  350. if (this.mZoomLevel > aRenderer.ZOOM_MAXLEVEL)
  351. this.mZoomLevel = aRenderer.ZOOM_MAXLEVEL;
  352. if (this.mZoomLevel < aRenderer.ZOOM_MINLEVEL)
  353. this.mZoomLevel = aRenderer.ZOOM_MINLEVEL;
  354. this.setZoomLevel(this.mZoomLevel); // Invalidates the map and zooms to
  355. // the maximum level of the
  356. // this.setZoomLevel(aRenderer.ZOOM_MAXLEVEL);// modified by cui 2013 4
  357. // 19
  358. // renderer.
  359. // added by cui 2013 4 19
  360. // GeoPoint p1 =
  361. // this.getProjection().fromPixels(MainMapActivity.Width/2,
  362. // MainMapActivity.Height/2,mBearing);
  363. // Toast.makeText(getContext(),
  364. // "p1="+p1.getLongitude()+".."+p1.getLatitude(),
  365. // Toast.LENGTH_LONG).show();
  366. // this.getController().animateTo(p1,OpenStreetMapViewController.AnimationType.MIDDLEPEAKSPEED,
  367. // OpenStreetMapViewController.ANIMATION_SMOOTHNESS_HIGH,
  368. // OpenStreetMapViewController.ANIMATION_DURATION_DEFAULT);
  369. // this.setZoomLevel(aRenderer.ZOOM_MAXLEVEL);
  370. // this.getController().animateTo(p1,OpenStreetMapViewController.AnimationType.MIDDLEPEAKSPEED,
  371. // OpenStreetMapViewController.ANIMATION_SMOOTHNESS_HIGH,OpenStreetMapViewController.ANIMATION_DURATION_DEFAULT);
  372. // end
  373. return ret;
  374. }
  375. public OpenStreetMapRendererInfo getRenderer() {
  376. return this.mRendererInfo;
  377. }
  378. /**
  379. * @param aZoomLevel
  380. * between 0 (equator) and 18/19(closest), depending on the
  381. * Renderer chosen.
  382. */
  383. public void setZoomLevel(final int aZoomLevel) {
  384. this.mZoomLevel = Math.max(this.mRendererInfo.ZOOM_MINLEVEL,
  385. Math.min(this.mRendererInfo.ZOOM_MAXLEVEL, aZoomLevel));
  386. if (this.mMiniMap != null) {
  387. if (this.mZoomLevel < this.mMiniMapZoomDiff) {
  388. if (this.mMiniMapOverriddenVisibility == NOT_SET)
  389. this.mMiniMap.setVisibility(View.INVISIBLE);
  390. } else {
  391. if (this.mMiniMapOverriddenVisibility == NOT_SET
  392. && this.mMiniMap.getVisibility() != View.VISIBLE) {
  393. this.mMiniMap.setVisibility(View.VISIBLE);
  394. }
  395. if (this.mMiniMapZoomDiff != NOT_SET)
  396. this.mMiniMap.setZoomLevel(this.mZoomLevel
  397. - this.mMiniMapZoomDiff);
  398. }
  399. }
  400. this.postInvalidate();
  401. }
  402. /**
  403. * Zooms in if possible.
  404. */
  405. public void zoomIn() {
  406. // final String nextBelowMaptileUrlString =
  407. // this.mRendererInfo.getTileURLString(Util
  408. // .getMapTileFromCoordinates(this.mLatitudeE6, this.mLongitudeE6,
  409. // this.mZoomLevel + 1,
  410. // null), this.mZoomLevel + 1);
  411. // this.mTileProvider.preCacheTile(nextBelowMaptileUrlString);
  412. this.setZoomLevel(this.mZoomLevel + 1);
  413. }
  414. /**
  415. * Zooms out if possible.
  416. */
  417. public void zoomOut() {
  418. this.setZoomLevel(this.mZoomLevel - 1);
  419. }
  420. /**
  421. * @return the current ZoomLevel between 0 (equator) and 18/19(closest),
  422. * depending on the Renderer chosen.
  423. */
  424. public int getZoomLevel() {
  425. return this.mZoomLevel;
  426. }
  427. public GeoPoint getMapCenter() {
  428. return new GeoPoint(this.mLatitudeE6, this.mLongitudeE6);
  429. }
  430. public int getMapCenterLatitudeE6() {
  431. return this.mLatitudeE6;
  432. }
  433. public int getMapCenterLongitudeE6() {
  434. return this.mLongitudeE6;
  435. }
  436. // ===========================================================
  437. // Methods from SuperClass/Interfaces
  438. // ===========================================================
  439. public void onLongPress(MotionEvent e) {
  440. for (OpenStreetMapViewOverlay osmvo : this.mOverlays)
  441. if (osmvo.onLongPress(e, this)) {
  442. mActionMoveDetected = true;
  443. return;
  444. }
  445. }
  446. public boolean onSingleTapUp(MotionEvent e) {
  447. for (OpenStreetMapViewOverlay osmvo : this.mOverlays)
  448. if (osmvo.onSingleTapUp(e, this))
  449. return true;
  450. return false;
  451. }
  452. public boolean onDoubleTap(MotionEvent e) {
  453. if (mBearing != 0) {
  454. mBearing = 0;
  455. Message.obtain(mMainActivityCallbackHandler, OpenStreetMapTileFilesystemProvider.MAP_ACTIVITY_MOVEMAP)
  456. .sendToTarget();
  457. } else {
  458. final GeoPoint newCenter = this.getProjection().fromPixels(
  459. e.getX(), e.getY());
  460. this.setMapCenter(newCenter);
  461. zoomIn();
  462. Message.obtain(mMainActivityCallbackHandler, OpenStreetMapTileFilesystemProvider.MAP_ACTIVITY_SETTITLE)
  463. .sendToTarget();
  464. }
  465. return true;
  466. }
  467. @Override
  468. public boolean onKeyDown(int keyCode, KeyEvent event) {
  469. // Log.e(DEBUGTAG, "onKeyDown keyCode="+keyCode);
  470. for (OpenStreetMapViewOverlay osmvo : this.mOverlays)
  471. if (osmvo.onKeyDown(keyCode, event, this))
  472. return true;
  473. return super.onKeyDown(keyCode, event);
  474. }
  475. @Override
  476. public boolean onKeyUp(int keyCode, KeyEvent event) {
  477. for (OpenStreetMapViewOverlay osmvo : this.mOverlays)
  478. if (osmvo.onKeyUp(keyCode, event, this))
  479. return true;
  480. return super.onKeyUp(keyCode, event);
  481. }
  482. @Override
  483. public boolean onTrackballEvent(MotionEvent event) {
  484. for (OpenStreetMapViewOverlay osmvo : this.mOverlays)
  485. if (osmvo.onTrackballEvent(event, this))
  486. return true;
  487. return super.onTrackballEvent(event);
  488. }
  489. public boolean canCreateContextMenu() {
  490. return !mActionMoveDetected;
  491. }
  492. public GeoPoint getTouchDownPoint() {
  493. return this.getProjection().fromPixels(mTouchDownX, mTouchDownY,
  494. mBearing);
  495. }
  496. boolean dialoif = true;
  497. boolean dialotsif = true;
  498. private class GestureCallback implements
  499. VersionedGestureDetector.OnGestureListener {
  500. public void onDown(MotionEvent event) {
  501. mActionMoveDetected = false;
  502. mStopMoveDetecting = false;
  503. OpenStreetMapView.this.mTouchDownX = (int) event.getX();
  504. OpenStreetMapView.this.mTouchDownY = (int) event.getY();
  505. //点击地图时,发送通知
  506. Message.obtain(mMainActivityCallbackHandler,
  507. OpenStreetMapTileFilesystemProvider.MAP_ACTIVITY_ONDOWN).sendToTarget();
  508. }
  509. public void onMove(MotionEvent event, int count, float x1, float y1,
  510. float x2, float y2) {
  511. if (Math.max(Math.abs(mTouchDownX - event.getX()),
  512. Math.abs(mTouchDownY - event.getY())) > 6
  513. && !mStopMoveDetecting) {
  514. mActionMoveDetected = true; // 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷
  515. // 锟斤拷锟斤拷锟斤拷锟斤拷
  516. // 锟斤拷锟�
  517. final float aRotateToAngle = 360 - mBearing;
  518. OpenStreetMapView.this.mTouchMapOffsetX = (int) (Math
  519. .sin(Math.toRadians(aRotateToAngle)) * (event
  520. .getY() - OpenStreetMapView.this.mTouchDownY))
  521. + (int) (Math.cos(Math.toRadians(aRotateToAngle)) * (event
  522. .getX() - OpenStreetMapView.this.mTouchDownX));
  523. OpenStreetMapView.this.mTouchMapOffsetY = (int) (Math
  524. .cos(Math.toRadians(aRotateToAngle)) * (event
  525. .getY() - OpenStreetMapView.this.mTouchDownY))
  526. - (int) (Math.sin(Math.toRadians(aRotateToAngle)) * (event
  527. .getX() - OpenStreetMapView.this.mTouchDownX));
  528. if (count > 1) {
  529. final double DiagonalSize = Math.hypot(
  530. (double) (x1 - x2), (double) (y1 - y2));
  531. mTouchScale = (DiagonalSize / mTouchDiagonalSize);
  532. }
  533. invalidate();
  534. Message.obtain(mMainActivityCallbackHandler,
  535. OpenStreetMapTileFilesystemProvider.MAP_ACTIVITY_MOVEMAP).sendToTarget();
  536. }
  537. }
  538. public void onUp(MotionEvent event) {
  539. mActionMoveDetected = false;
  540. mStopMoveDetecting = true;
  541. final int viewWidth_2 = OpenStreetMapView.this.getWidth() / 2;
  542. final int viewHeight_2 = OpenStreetMapView.this.getHeight() / 2;
  543. final GeoPoint newCenter = OpenStreetMapView.this.getProjection()
  544. .fromPixels(viewWidth_2, viewHeight_2);
  545. OpenStreetMapView.this.mTouchMapOffsetX = 0;
  546. OpenStreetMapView.this.mTouchMapOffsetY = 0;
  547. OpenStreetMapView.this.setMapCenter(newCenter); // Calls invalidate
  548. }
  549. public void onDown2(MotionEvent event, float x1, float y1, float x2,
  550. float y2) {
  551. mTouchDiagonalSize = Math.hypot((double) (x1 - x2),
  552. (double) (y1 - y2));
  553. mActionMoveDetected = true;
  554. }
  555. public void onUp2(MotionEvent event) {
  556. if (mTouchScale > 1)
  557. setZoomLevel(getZoomLevel() + (int) Math.round(mTouchScale) - 1);
  558. else
  559. setZoomLevel(getZoomLevel() - (int) Math.round(1 / mTouchScale)
  560. + 1);
  561. mTouchScale = 1;
  562. mActionMoveDetected = false;
  563. mStopMoveDetecting = true;
  564. final GeoPoint newCenter2 = OpenStreetMapView.this.getProjection()
  565. .fromPixels(OpenStreetMapView.this.getWidth() / 2,
  566. OpenStreetMapView.this.getHeight() / 2);
  567. OpenStreetMapView.this.mTouchMapOffsetX = 0;
  568. OpenStreetMapView.this.mTouchMapOffsetY = 0;
  569. OpenStreetMapView.this.setMapCenter(newCenter2); // Calls invalidate
  570. Message.obtain(mMainActivityCallbackHandler, OpenStreetMapTileFilesystemProvider.MAP_ACTIVITY_SETTITLE)
  571. .sendToTarget();
  572. }
  573. }
  574. public void setClickable(boolean clickble) {
  575. this.clickable = clickble;
  576. }
  577. @Override
  578. public boolean onTouchEvent(final MotionEvent event) {
  579. if (OpenStreetMapView.this.clickable) {
  580. for (OpenStreetMapViewOverlay osmvo : this.mOverlays)
  581. if (osmvo.onTouchEvent(event, this))
  582. return true;
  583. this.mGestureDetector.onTouchEvent(event);
  584. this.mDetector.onTouchEvent(event);
  585. return super.onTouchEvent(event);
  586. }
  587. return false;
  588. }
  589. /*=======
  590. ScrollView scrollPanel = new ScrollView(context);
  591. scrollPanel.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
  592. LayoutParams.FILL_PARENT));
  593. LinearLayout yiPanel = new LinearLayout(context);
  594. yiPanel.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
  595. LayoutParams.WRAP_CONTENT));
  596. yiPanel.setOrientation(LinearLayout.HORIZONTAL);
  597. TextView mbmc = new TextView(context);
  598. mbmc.setText("态势名称:");
  599. TextView mbmc2 = new TextView(context);
  600. mbmc2.setText(ts.getName());
  601. yiPanel.addView(mbmc);
  602. yiPanel.addView(mbmc2);
  603. LinearLayout sanPanel = new LinearLayout(context);
  604. sanPanel.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
  605. LayoutParams.WRAP_CONTENT));
  606. sanPanel.setOrientation(LinearLayout.HORIZONTAL);
  607. TextView ZXJD = new TextView(context);
  608. ZXJD.setText("经度:");
  609. TextView ZXJD2 = new TextView(context);
  610. ZXJD2.setText(ts.getJingdu());
  611. sanPanel.addView(ZXJD);
  612. sanPanel.addView(ZXJD2);
  613. LinearLayout siPanel = new LinearLayout(context);
  614. siPanel.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
  615. LayoutParams.WRAP_CONTENT));
  616. siPanel.setOrientation(LinearLayout.HORIZONTAL);
  617. TextView ZXWD = new TextView(context);
  618. ZXWD.setText("纬度:");
  619. TextView ZXWD2 = new TextView(context);
  620. ZXWD2.setText(ts.getWeidu());
  621. siPanel.addView(ZXWD);
  622. siPanel.addView(ZXWD2);
  623. LinearLayout wuPanel = new LinearLayout(context);
  624. wuPanel.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
  625. LayoutParams.WRAP_CONTENT));
  626. wuPanel.setOrientation(LinearLayout.HORIZONTAL);
  627. TextView MBMS = new TextView(context);
  628. MBMS.setText("态势描述:");
  629. TextView MBMS2 = new TextView(context);
  630. MBMS2.setText(ts.getDes());
  631. wuPanel.addView(MBMS);
  632. wuPanel.addView(MBMS2);
  633. // 总layer
  634. LinearLayout mainPanel = new LinearLayout(context);
  635. mainPanel.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
  636. LayoutParams.WRAP_CONTENT));
  637. mainPanel.setOrientation(LinearLayout.VERTICAL);
  638. LinearLayout liuPanel = new LinearLayout(context);
  639. liuPanel.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
  640. LayoutParams.WRAP_CONTENT));
  641. liuPanel.setOrientation(LinearLayout.HORIZONTAL);
  642. Button button = new Button(context);
  643. button.setText("关闭");
  644. button.setWidth(100);
  645. button.setHeight(40);
  646. button.setOnClickListener(new OnClickListener() {
  647. public void onClick(View v) {
  648. // TODO Auto-generated method stub
  649. infoDialog.dismiss();
  650. dialotsif = true;
  651. }
  652. });
  653. liuPanel.addView(button);
  654. mainPanel.addView(yiPanel);
  655. mainPanel.addView(siPanel);
  656. mainPanel.addView(sanPanel);
  657. mainPanel.addView(wuPanel);
  658. mainPanel.addView(liuPanel);
  659. scrollPanel.addView(mainPanel);
  660. infoDialog.setContentView(scrollPanel);
  661. infoDialog.show();
  662. }
  663. public void selectInfoDetail(String id) {
  664. // TODO Auto-generated method stub
  665. int MBID = new Integer(id).intValue();
  666. TargetManagerDao mTargetManagerDao=new TargetManagerDao(context);
  667. final Cursor cursor =mTargetManagerDao.find(MBID);
  668. if (cursor.moveToFirst()) {
  669. final Dialog infoDialog;
  670. infoDialog = new Dialog(context);
  671. infoDialog.setCanceledOnTouchOutside(false);// modified by cui
  672. // 2012 12 10
  673. infoDialog.setCancelable(false);// modified by cui 2012 12 10
  674. infoDialog
  675. .setTitle(cursor.getString(cursor.getColumnIndex("MBMC")));
  676. ScrollView scrollPanel = new ScrollView(context);
  677. scrollPanel.setLayoutParams(new LayoutParams(
  678. LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
  679. LinearLayout yiPanel = new LinearLayout(context);
  680. yiPanel.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
  681. LayoutParams.WRAP_CONTENT));
  682. yiPanel.setOrientation(LinearLayout.HORIZONTAL);
  683. TextView mbmc = new TextView(context);
  684. mbmc.setText("目标名称:");
  685. TextView mbmc2 = new TextView(context);
  686. mbmc2.setText(cursor.getString(cursor.getColumnIndex("MBMC")));
  687. yiPanel.addView(mbmc);
  688. yiPanel.addView(mbmc2);
  689. LinearLayout erPanel = new LinearLayout(context);
  690. erPanel.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
  691. LayoutParams.WRAP_CONTENT));
  692. erPanel.setOrientation(LinearLayout.HORIZONTAL);
  693. TextView MBLB = new TextView(context);
  694. MBLB.setText("目标类别:");
  695. TextView MBLB2 = new TextView(context);
  696. MBLB2.setText(cursor.getString(cursor.getColumnIndex("MBLB")));
  697. erPanel.addView(MBLB);
  698. erPanel.addView(MBLB2);
  699. LinearLayout sanPanel = new LinearLayout(context);
  700. sanPanel.setLayoutParams(new LayoutParams(
  701. LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
  702. sanPanel.setOrientation(LinearLayout.HORIZONTAL);
  703. TextView DLWZ = new TextView(context);
  704. DLWZ.setText("地理位置:");
  705. TextView DLWZ2 = new TextView(context);
  706. DLWZ2.setText(cursor.getString(cursor.getColumnIndex("DLWZ")));
  707. sanPanel.addView(DLWZ);
  708. sanPanel.addView(DLWZ2);
  709. LinearLayout siPanel = new LinearLayout(context);
  710. siPanel.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
  711. LayoutParams.WRAP_CONTENT));
  712. siPanel.setOrientation(LinearLayout.HORIZONTAL);
  713. TextView ZXJD = new TextView(context);
  714. ZXJD.setText("中心点经度:");
  715. TextView ZXJD2 = new TextView(context);
  716. ZXJD2.setText(cursor.getString(cursor.getColumnIndex("ZXJD")));
  717. siPanel.addView(ZXJD);
  718. siPanel.addView(ZXJD2);
  719. TextView ZXWD = new TextView(context);
  720. ZXWD.setText("中心点纬度:");
  721. TextView ZXWD2 = new TextView(context);
  722. ZXWD2.setText(cursor.getString(cursor.getColumnIndex("ZXWD")));
  723. siPanel.addView(ZXWD);
  724. siPanel.addView(ZXWD2);
  725. LinearLayout wuPanel = new LinearLayout(context);
  726. wuPanel.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
  727. LayoutParams.WRAP_CONTENT));
  728. wuPanel.setOrientation(LinearLayout.HORIZONTAL);
  729. TextView MBMS = new TextView(context);
  730. MBMS.setText("目标描述:");
  731. TextView MBMS2 = new TextView(context);
  732. MBMS2.setText(cursor.getString(cursor.getColumnIndex("MBMS")));
  733. wuPanel.addView(MBMS);
  734. wuPanel.addView(MBMS2);
  735. // //总layer
  736. LinearLayout mainPanel = new LinearLayout(context);
  737. mainPanel.setLayoutParams(new LayoutParams(
  738. LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
  739. mainPanel.setOrientation(LinearLayout.VERTICAL);
  740. LinearLayout liuPanel = new LinearLayout(context);
  741. liuPanel.setLayoutParams(new LayoutParams(
  742. LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
  743. liuPanel.setOrientation(LinearLayout.HORIZONTAL);
  744. final String datapath = cursor.getString(cursor
  745. .getColumnIndex("DATAPATH"));
  746. Button tbtn = new Button(context);
  747. tbtn.setWidth(100);
  748. tbtn.setHeight(40);
  749. tbtn.setText("查看");
  750. tbtn.setOnClickListener(new OnClickListener() {
  751. public void onClick(View v) {
  752. // TODO Auto-generated method stub
  753. bh(cursor.getString(cursor.getColumnIndex("DATAPATH")));
  754. infoDialog.dismiss();
  755. dialoif = true;
  756. }
  757. });
  758. Button sbtn = new Button(context);
  759. sbtn.setWidth(85);
  760. sbtn.setHeight(40);
  761. sbtn.setText("查看");
  762. sbtn.setOnClickListener(new OnClickListener() {
  763. public void onClick(View v) {
  764. // TODO Auto-generated method stub
  765. huizhi2(cursor.getString(cursor.getColumnIndex("MBMC"))
  766. + ".xml");
  767. infoDialog.dismiss();
  768. // dialoif = true;
  769. }
  770. });
  771. Button seebutton = new Button(context);
  772. seebutton.setWidth(100);
  773. seebutton.setHeight(40);
  774. seebutton.setText("查看");
  775. seebutton.setOnClickListener(new OnClickListener() {
  776. public void onClick(View v) {
  777. // TODO Auto-generated method stub
  778. // 获取路径信息
  779. InfoResourceAdapter.setFilePath(datapath);
  780. Log.i("DDDD", datapath);
  781. if (datapath.substring(datapath.lastIndexOf(".") + 1)
  782. .equals("3gp")) {
  783. Intent i = new Intent(context, InfoVideoUI.class);
  784. context.startActivity(i);
  785. } else if (datapath
  786. .substring(datapath.lastIndexOf(".") + 1).equals(
  787. "jpg")) {
  788. Intent i = new Intent(context, InfoImageUI.class);
  789. context.startActivity(i);
  790. } else if (datapath
  791. .substring(datapath.lastIndexOf(".") + 1).equals(
  792. "amr")) {
  793. Intent i = new Intent(context, InfoVideoUI.class);
  794. context.startActivity(i);
  795. } else if (datapath
  796. .substring(datapath.lastIndexOf(".") + 1).equals(
  797. "xml")) {
  798. Intent i = new Intent(context, InfoDetailUI.class);
  799. context.startActivity(i);
  800. }
  801. infoDialog.dismiss();
  802. dialoif = true;
  803. }
  804. });
  805. Button button = new Button(context);
  806. button.setText("关闭");
  807. button.setWidth(100);
  808. button.setHeight(40);
  809. button.setOnClickListener(new OnClickListener() {
  810. public void onClick(View v) {
  811. // TODO Auto-generated method stub
  812. infoDialog.dismiss();
  813. dialoif = true;
  814. }
  815. });
  816. if (datapath.substring(datapath.lastIndexOf(".") + 1).equals("3gp")
  817. || datapath.substring(datapath.lastIndexOf(".") + 1)
  818. .equals("jpg")
  819. || datapath.substring(datapath.lastIndexOf(".") + 1)
  820. .equals("amr")) {
  821. liuPanel.addView(seebutton);
  822. }
  823. if (cursor.getString(cursor.getColumnIndex("MBLB")).equals("矢量情报"))
  824. liuPanel.addView(sbtn);
  825. if (cursor.getString(cursor.getColumnIndex("MBLB")).equals("态势情报"))
  826. liuPanel.addView(tbtn);
  827. liuPanel.addView(button);
  828. mainPanel.addView(yiPanel);
  829. mainPanel.addView(erPanel);
  830. if (!cursor.getString(cursor.getColumnIndex("MBLB")).equals("矢量情报")) {
  831. mainPanel.addView(sanPanel);
  832. }
  833. mainPanel.addView(wuPanel);
  834. mainPanel.addView(siPanel);
  835. mainPanel.addView(liuPanel);
  836. scrollPanel.addView(mainPanel);
  837. infoDialog.setContentView(scrollPanel);
  838. infoDialog.show();
  839. }
  840. }
  841. protected void bh(String pathname) {
  842. // TODO Auto-generated method stub
  843. File f = new File("/sdcard/gtMaps/signs/signs.sqlitedb");
  844. if (!f.exists()) {
  845. Toast.makeText(getContext(), "军标图标路径不存在!", 0).show();
  846. return;
  847. }
  848. terminalXmlParse.XmlParseToMap xm = new terminalXmlParse.XmlParseToMap();
  849. HashMap<String, String> xmlValue = new HashMap<String, String>();
  850. List<DrawTuchArmy> lst2 = new ArrayList<DrawTuchArmy>();
  851. try {
  852. xm.readDoc(pathname, xmlValue, lst2);
  853. } catch (FileNotFoundException e) {
  854. // TODO Auto-generated catch block
  855. e.printStackTrace();
  856. } catch (DocumentException e) {
  857. // TODO Auto-generated catch block
  858. e.printStackTrace();
  859. }
  860. for (int i = 0; i < lst2.size(); i++) {
  861. DrawOverlay.armysign = true;
  862. QYConstants.listDTA2.add(lst2.get(i));
  863. Log.i("TS1", lst2.get(i).getName() + ".."
  864. + lst2.get(i).getMarkname() + ".."
  865. + lst2.get(i).getJingdu() + "...." + lst2.get(i).getWeidu());
  866. }
  867. invalidate();
  868. }
  869. protected void huizhi2(String pathname) {
  870. // TODO Auto-generated method stub
  871. DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();
  872. try {
  873. DocumentBuilder dombuilder = domfac.newDocumentBuilder();
  874. InputStream in = new FileInputStream(
  875. Environment.getExternalStorageDirectory()
  876. + "/gtMaps/qb/slqb/" + pathname);
  877. Document doc = dombuilder.parse(in);
  878. Element root = doc.getDocumentElement();
  879. NodeList books = root.getChildNodes();
  880. Log.i("子节点数量", "" + books.getLength());
  881. if (books != null) {
  882. for (int i = 0; i < books.getLength(); i++) {
  883. Node book = books.item(i);
  884. Log.i("子节点名称:", "" + book.getNodeName());
  885. if (book.getNodeName().equals("Point")) {
  886. String jing = null;
  887. String wei = null;
  888. NodeList childnodes = book.getChildNodes();
  889. for (int n = 0; n < childnodes.getLength(); n++) {
  890. Node childnode = childnodes.item(n);
  891. if (childnode.getNodeName().equals("jingdu")) {
  892. jing = childnode.getFirstChild().getNodeValue();
  893. }
  894. if (childnode.getNodeName().equals("weidu")) {
  895. wei = childnode.getFirstChild().getNodeValue();
  896. }
  897. }
  898. GeoPoint geopoint = GeoPoint.from2DoubleString(wei,
  899. jing);
  900. DrawOverlay.points.add(geopoint);
  901. invalidate();
  902. }
  903. if (book.getNodeName().equals("Line")) {
  904. NodeList childnodes = book.getChildNodes();
  905. DrawOverlay.line = new ArrayList<GeoPoint>();
  906. for (int n = 0; n < childnodes.getLength(); n++) {
  907. String jing2 = null;
  908. String wei2 = null;
  909. GeoPoint geopoint = null;
  910. Node childnode = childnodes.item(n);
  911. NodeList aa = childnode.getChildNodes();
  912. for (int m = 0; m < aa.getLength(); m++) {
  913. Node bb = aa.item(m);
  914. if (bb.getNodeName().equals("jingdu")) {
  915. jing2 = bb.getFirstChild().getNodeValue();
  916. }
  917. if (bb.getNodeName().equals("weidu")) {
  918. wei2 = bb.getFirstChild().getNodeValue();
  919. }
  920. }
  921. geopoint = GeoPoint.from2DoubleString(wei2, jing2);
  922. DrawOverlay.line.add(geopoint);
  923. }
  924. DrawOverlay.lines.add(DrawOverlay.line);
  925. }
  926. if (book.getNodeName().equals("Mian")) {
  927. NodeList childnodes = book.getChildNodes();
  928. DrawOverlay.cover = new ArrayList<GeoPoint>();
  929. for (int n = 0; n < childnodes.getLength(); n++) {
  930. String jing2 = null;
  931. String wei2 = null;
  932. GeoPoint geopoint = null;
  933. Node childnode = childnodes.item(n);
  934. NodeList aa = childnode.getChildNodes();
  935. for (int m = 0; m < aa.getLength(); m++) {
  936. Node bb = aa.item(m);
  937. if (bb.getNodeName().equals("jingdu")) {
  938. jing2 = bb.getFirstChild().getNodeValue();
  939. }
  940. if (bb.getNodeName().equals("weidu")) {
  941. wei2 = bb.getFirstChild().getNodeValue();
  942. }
  943. }
  944. geopoint = GeoPoint.from2DoubleString(wei2, jing2);
  945. DrawOverlay.cover.add(geopoint);
  946. }
  947. DrawOverlay.covers.add(DrawOverlay.cover);
  948. }
  949. this.invalidate();
  950. }
  951. }
  952. } catch (ParserConfigurationException e) {
  953. // TODO Auto-generated catch block
  954. e.printStackTrace();
  955. } catch (FileNotFoundException e) {
  956. // TODO Auto-generated catch block
  957. e.printStackTrace();
  958. } catch (SAXException e) {
  959. // TODO Auto-generated catch block
  960. e.printStackTrace();
  961. } catch (IOException e) {
  962. // TODO Auto-generated catch block
  963. e.printStackTrace();
  964. }
  965. }
  966. >>>>>>> .r2109*/
  967. @Override
  968. public void onDraw(final Canvas c) {
  969. //MainMapActivity.canva = c;
  970. /*
  971. * Do some calculations and drag attributes to local variables to save
  972. * some performance.
  973. */
  974. if(this.mRendererInfo==null) return;
  975. if (this.mZoomLevel >= 18) {
  976. this.mZoomLevel = 18;
  977. // Toast.makeText(OpenStreetMapView.this, "目前已放到最大", 1).show();
  978. }
  979. final int zoomLevel = this.mZoomLevel;
  980. final int tileSizePxNotScale = this.mRendererInfo
  981. .getTileSizePx(this.mZoomLevel);
  982. final int tileSizePx = (int) (tileSizePxNotScale * mTouchScale);
  983. c.save();
  984. final float aRotateToAngle = 360 - mBearing;
  985. c.rotate(aRotateToAngle, this.getWidth() / 2, this.getHeight() / 2);
  986. c.drawRGB(255, 255, 255);
  987. final int[] centerMapTileCoords = Util.getMapTileFromCoordinates(
  988. this.mLatitudeE6, this.mLongitudeE6, zoomLevel, null,
  989. this.mRendererInfo.PROJECTION);
  990. /*
  991. * Calculate the Latitude/Longitude on the left-upper ScreenCoords of
  992. * the center MapTile. So in the end we can determine which MapTiles we
  993. * additionally need next to the centerMapTile.
  994. */
  995. final Point upperLeftCornerOfCenterMapTileNotScale = getUpperLeftCornerOfCenterMapTileInScreen(
  996. centerMapTileCoords, tileSizePxNotScale, null);
  997. final int centerMapTileScreenLeftNotScale = upperLeftCornerOfCenterMapTileNotScale.x;
  998. final int centerMapTileScreenTopNotScale = upperLeftCornerOfCenterMapTileNotScale.y;
  999. final int centerMapTileScreenRightNotScale = centerMapTileScreenLeftNotScale
  1000. + tileSizePxNotScale;
  1001. final int centerMapTileScreenBottomNotScale = centerMapTileScreenTopNotScale
  1002. + tileSizePxNotScale;
  1003. final Point upperLeftCornerOfCenterMapTile = getUpperLeftCornerOfCenterMapTileInScreen(
  1004. centerMapTileCoords, tileSizePx, null);
  1005. final int centerMapTileScreenLeft = upperLeftCornerOfCenterMapTile.x;
  1006. final int centerMapTileScreenTop = upperLeftCornerOfCenterMapTile.y;
  1007. /*
  1008. * Calculate the amount of tiles needed for each side around the center
  1009. * one.
  1010. */
  1011. // TODO 锟斤拷锟斤拷锟�锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷 锟斤拷锟斤拷锟斤拷锟斤拷 锟斤拷锟�锟斤拷锟斤拷锟斤拷
  1012. // 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟�锟斤拷锟斤拷锟斤拷, 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷
  1013. // 锟�锟斤拷锟斤拷锟�锟斤拷锟�锟斤拷锟斤拷锟斤拷锟斤拷 锟斤拷锟斤拷锟�
  1014. final int iDelta = (mBearing > 0 && mRendererInfo.YANDEX_TRAFFIC_ON == 0) ? 1
  1015. : 0;
  1016. /*Log.i("iDelta",
  1017. "iDelta=======" + iDelta + "this.getHeight()="
  1018. + this.getHeight() + "this.getwidth()="
  1019. + this.getWidth() + "" + MainMapActivity.Width + ".."
  1020. + MainMapActivity.Height);*/
  1021. // int additionalTilesNeededToLeftOfCenter =((int) Math
  1022. // .ceil((float) centerMapTileScreenLeftNotScale
  1023. // / tileSizePxNotScale))
  1024. // + iDelta; // i.e.
  1025. //
  1026. // int additionalTilesNeededToRightOfCenter =((int) Math
  1027. // .ceil((float) (this.getWidth() - centerMapTileScreenRightNotScale)
  1028. // / tileSizePxNotScale))
  1029. // + iDelta;
  1030. // int additionalTilesNeededToTopOfCenter = ((int) Math
  1031. // .ceil((float) centerMapTileScreenTopNotScale
  1032. // / tileSizePxNotScale))+iDelta; // i.e.
  1033. // int additionalTilesNeededToBottomOfCenter = ((int) Math
  1034. // .ceil((float) (this.getHeight() - centerMapTileScreenBottomNotScale)
  1035. // / tileSizePxNotScale))+iDelta;
  1036. // if (MainMapActivity.Height == 1280) {
  1037. // int max1 = Math.max(additionalTilesNeededToTopOfCenter,
  1038. // additionalTilesNeededToBottomOfCenter);
  1039. // int max2 = Math.max(additionalTilesNeededToRightOfCenter,
  1040. // additionalTilesNeededToLeftOfCenter);
  1041. // if(max1==max2&&max1!=2){
  1042. // if(additionalTilesNeededToRightOfCenter>additionalTilesNeededToLeftOfCenter){
  1043. // additionalTilesNeededToRightOfCenter =
  1044. // additionalTilesNeededToRightOfCenter-1;
  1045. // }else{
  1046. // additionalTilesNeededToLeftOfCenter =
  1047. // additionalTilesNeededToLeftOfCenter-1;
  1048. // }
  1049. // }
  1050. // }else if(MainMapActivity.Height==960){
  1051. // int max1 = Math.max(additionalTilesNeededToTopOfCenter,
  1052. // additionalTilesNeededToBottomOfCenter);
  1053. // int max2 = Math.max(additionalTilesNeededToRightOfCenter,
  1054. // additionalTilesNeededToLeftOfCenter);
  1055. // if(max1==max2&&max1!=2){
  1056. // if(additionalTilesNeededToRightOfCenter>additionalTilesNeededToLeftOfCenter){
  1057. // additionalTilesNeededToRightOfCenter =
  1058. // additionalTilesNeededToRightOfCenter-1;
  1059. // }else{
  1060. // additionalTilesNeededToLeftOfCenter =
  1061. // additionalTilesNeededToLeftOfCenter-1;
  1062. // }
  1063. // }
  1064. // // additionalTilesNeededToLeftOfCenter =
  1065. // additionalTilesNeededToLeftOfCenter+1;
  1066. // // additionalTilesNeededToRightOfCenter =
  1067. // additionalTilesNeededToRightOfCenter+1;
  1068. // }
  1069. int additionalTilesNeededToLeftOfCenter = 2;
  1070. int additionalTilesNeededToRightOfCenter = 2;
  1071. int additionalTilesNeededToBottomOfCenter = 2;
  1072. int additionalTilesNeededToTopOfCenter = 2;
  1073. /*if (MainMapActivity.Height == 960 || MainMapActivity.Height == 540) {
  1074. additionalTilesNeededToLeftOfCenter = 2;
  1075. additionalTilesNeededToRightOfCenter = 2;
  1076. additionalTilesNeededToBottomOfCenter = 2;
  1077. additionalTilesNeededToTopOfCenter = 2;
  1078. }*/
  1079. // if(MainMapActivity.Height==720){
  1080. // additionalTilesNeededToLeftOfCenter = 2;
  1081. // additionalTilesNeededToRightOfCenter=3;
  1082. // additionalTilesNeededToBottomOfCenter=2;
  1083. // additionalTilesNeededToTopOfCenter=2;
  1084. // }
  1085. // if(MainMapActivity.Height==1280){
  1086. // additionalTilesNeededToLeftOfCenter = 2;
  1087. // additionalTilesNeededToRightOfCenter=2;
  1088. // additionalTilesNeededToBottomOfCenter=3;
  1089. // additionalTilesNeededToTopOfCenter=2;
  1090. // }
  1091. final int mapTileUpperBound = mRendererInfo
  1092. .getTileUpperBound(zoomLevel);
  1093. final int[] mapTileCoords = new int[] {
  1094. centerMapTileCoords[MAPTILE_LATITUDE_INDEX],
  1095. centerMapTileCoords[MAPTILE_LONGITUDE_INDEX] };
  1096. Log.i("TipCount", "Top=" + additionalTilesNeededToTopOfCenter
  1097. + "..button=" + additionalTilesNeededToBottomOfCenter
  1098. + "..left=" + additionalTilesNeededToLeftOfCenter + "..right="
  1099. + additionalTilesNeededToRightOfCenter);
  1100. int countY = 0;
  1101. /* Draw all the MapTiles (from the upper left to the lower right). */
  1102. for (int y = -additionalTilesNeededToTopOfCenter; y <= additionalTilesNeededToBottomOfCenter; y++) {
  1103. countY++;
  1104. int countX = 0;
  1105. for (int x = -additionalTilesNeededToLeftOfCenter; x <= additionalTilesNeededToRightOfCenter; x++) {
  1106. /*
  1107. * Add/substract the difference of the tile-position to the one
  1108. * of the center.
  1109. */
  1110. mapTileCoords[MAPTILE_LATITUDE_INDEX] = MyMath.mod(
  1111. centerMapTileCoords[MAPTILE_LATITUDE_INDEX] + y,
  1112. mapTileUpperBound);
  1113. mapTileCoords[MAPTILE_LONGITUDE_INDEX] = MyMath.mod(
  1114. centerMapTileCoords[MAPTILE_LONGITUDE_INDEX] + x,
  1115. mapTileUpperBound);
  1116. /* Construct a URLString, which represents the MapTile. */
  1117. final String tileURLString = this.mRendererInfo
  1118. .getTileURLString(mapTileCoords, zoomLevel);
  1119. // Ut.dd("onDraw: " + tileURLString);
  1120. /* Draw the MapTile 'i tileSizePx' above of the centerMapTile */
  1121. final Bitmap currentMapTile = this.mTileProvider.getMapTile(
  1122. tileURLString, this.mRendererInfo.TILE_SOURCE_TYPE,
  1123. mapTileCoords[MAPTILE_LONGITUDE_INDEX],
  1124. mapTileCoords[MAPTILE_LATITUDE_INDEX], zoomLevel);
  1125. if (currentMapTile != null) {
  1126. final int tileLeft = this.mTouchMapOffsetX
  1127. + centerMapTileScreenLeft + (x * tileSizePx);
  1128. final int tileTop = this.mTouchMapOffsetY
  1129. + centerMapTileScreenTop + (y * tileSizePx);
  1130. final Rect r = new Rect(tileLeft, tileTop, tileLeft
  1131. + tileSizePx + 2, tileTop + tileSizePx + 2);
  1132. if (!currentMapTile.isRecycled()) {
  1133. countX++;
  1134. c.drawBitmap(currentMapTile, null, r, this.mPaint);
  1135. Log.i("drawBitmap", currentMapTile + "");
  1136. }
  1137. if (DEBUGMODE) {
  1138. c.drawLine(tileLeft, tileTop, tileLeft + tileSizePx,
  1139. tileTop, this.mPaint);
  1140. c.drawLine(tileLeft, tileTop, tileLeft, tileTop
  1141. + tileSizePx, this.mPaint);
  1142. c.drawText(
  1143. "y x = "
  1144. + mapTileCoords[MAPTILE_LATITUDE_INDEX]
  1145. + " "
  1146. + mapTileCoords[MAPTILE_LONGITUDE_INDEX]
  1147. + " zoom "
  1148. + zoomLevel
  1149. + " "
  1150. + mRendererInfo
  1151. .getQRTS(
  1152. mapTileCoords[MAPTILE_LONGITUDE_INDEX],
  1153. mapTileCoords[MAPTILE_LATITUDE_INDEX],
  1154. zoomLevel),
  1155. tileLeft + 5, tileTop + 15, this.mPaint);
  1156. }
  1157. }
  1158. }
  1159. countY++;
  1160. }
  1161. // this.mTileProvider.CommitCash();
  1162. /* Draw all Overlays. */
  1163. for (OpenStreetMapViewOverlay osmvo : this.mOverlays)
  1164. osmvo.onManagedDraw(c, this);
  1165. this.mPaint.setStyle(Style.STROKE);
  1166. if (this.mMaxiMap != null) // If this is a MiniMap
  1167. c.drawRect(0, 0, this.getWidth() - 1, this.getHeight() - 1,
  1168. this.mPaint);
  1169. c.restore();
  1170. // c.drawLine(viewWidth/2, 0, viewWidth/2, viewHeight, this.mPaint);
  1171. // c.drawLine(0, viewHeight/2, viewWidth, viewHeight/2, this.mPaint);
  1172. // c.drawCircle(viewWidth/2, viewHeight/2, 100, this.mPaint);
  1173. // c.drawLine(viewWidth/2-100, viewHeight/2-100, viewWidth/2+100,
  1174. // viewHeight/2+100, this.mPaint);
  1175. // c.drawLine(viewWidth/2+100, viewHeight/2-100, viewWidth/2-100,
  1176. // viewHeight/2+100, this.mPaint);
  1177. }
  1178. // ===========================================================
  1179. // Methods
  1180. // ===========================================================
  1181. /**
  1182. * @param centerMapTileCoords
  1183. * @param tileSizePx
  1184. * @param reuse
  1185. * just pass null if you do not have a Point to be 'recycled'.
  1186. */
  1187. private Point getUpperLeftCornerOfCenterMapTileInScreen(
  1188. final int[] centerMapTileCoords, final int tileSizePx,
  1189. final Point reuse) {
  1190. final Point out = (reuse != null) ? reuse : new Point();
  1191. final int viewWidth = this.getWidth();
  1192. final int viewWidth_2 = viewWidth / 2;
  1193. final int viewHeight = this.getHeight();
  1194. final int viewHeight_2 = viewHeight / 2;
  1195. /*
  1196. * Calculate the Latitude/Longitude on the left-upper ScreenCoords of
  1197. * the center MapTile. So in the end we can determine which MapTiles we
  1198. * additionally need next to the centerMapTile.
  1199. */
  1200. final BoundingBoxE6 bb = Util.getBoundingBoxFromMapTile(
  1201. centerMapTileCoords, this.mZoomLevel, mRendererInfo.PROJECTION);
  1202. final float[] relativePositionInCenterMapTile = bb
  1203. .getRelativePositionOfGeoPointInBoundingBoxWithLinearInterpolation(
  1204. this.mLatitudeE6, this.mLongitudeE6, null);
  1205. final int centerMapTileScreenLeft = viewWidth_2
  1206. - (int) (0.5f + (relativePositionInCenterMapTile[MAPTILE_LONGITUDE_INDEX] * tileSizePx));
  1207. final int centerMapTileScreenTop = viewHeight_2
  1208. - (int) (0.5f + (relativePositionInCenterMapTile[MAPTILE_LATITUDE_INDEX] * tileSizePx));
  1209. out.set(centerMapTileScreenLeft, centerMapTileScreenTop);
  1210. return out;
  1211. }
  1212. // ===========================================================
  1213. // Inner and Anonymous Classes
  1214. // ===========================================================
  1215. /**
  1216. * This class may return valid results until the underlying
  1217. * {@link OpenStreetMapView} gets modified in any way (i.e. new center).
  1218. *
  1219. * @author Nicolas Gramlich
  1220. */
  1221. public class OpenStreetMapViewProjection {
  1222. final int viewWidth;
  1223. final int viewHeight;
  1224. final BoundingBoxE6 bb;
  1225. final int zoomLevel;
  1226. final int tileSizePx;
  1227. final int[] centerMapTileCoords;
  1228. final Point upperLeftCornerOfCenterMapTile;
  1229. public OpenStreetMapViewProjection() {
  1230. viewWidth = OpenStreetMapView.this.getWidth();
  1231. viewHeight = OpenStreetMapView.this.getHeight();
  1232. /*
  1233. * Do some calculations and drag attributes to local variables to
  1234. * save some performance.
  1235. */
  1236. zoomLevel = OpenStreetMapView.this.mZoomLevel; // LATER Draw to
  1237. // attributes and so
  1238. // make it only
  1239. // 'valid' for a
  1240. // short time.
  1241. tileSizePx = (int) (OpenStreetMapView.this.mRendererInfo
  1242. .getTileSizePx(OpenStreetMapView.this.mZoomLevel) * OpenStreetMapView.this.mTouchScale);
  1243. /*
  1244. * Get the center MapTile which is above this.mLatitudeE6 and
  1245. * this.mLongitudeE6 .
  1246. */
  1247. centerMapTileCoords = Util.getMapTileFromCoordinates(
  1248. OpenStreetMapView.this.mLatitudeE6,
  1249. OpenStreetMapView.this.mLongitudeE6, zoomLevel, null,
  1250. OpenStreetMapView.this.mRendererInfo.PROJECTION);
  1251. upperLeftCornerOfCenterMapTile = getUpperLeftCornerOfCenterMapTileInScreen(
  1252. centerMapTileCoords, tileSizePx, null);
  1253. bb = OpenStreetMapView.this.getDrawnBoundingBoxE6();
  1254. }
  1255. /**
  1256. * Converts x/y ScreenCoordinates to the underlying GeoPoint.
  1257. *
  1258. * @param x
  1259. * @param y
  1260. * @return GeoPoint under x/y.
  1261. */
  1262. public GeoPoint fromPixels(float x, float y) {
  1263. /* Subtract the offset caused by touch. */
  1264. // Log.d(DEBUGTAG,
  1265. // "x = "+x+" mTouchMapOffsetX = "+OpenStreetMapView.this.mTouchMapOffsetX+" ");
  1266. x -= OpenStreetMapView.this.mTouchMapOffsetX;
  1267. y -= OpenStreetMapView.this.mTouchMapOffsetY;
  1268. // int xx =
  1269. // centerMapTileCoords[0]*tileSizePx+(int)x-upperLeftCornerOfCenterMapTile.x;
  1270. // int asd = Util.x2lon(xx, zoomLevel, tileSizePx);
  1271. GeoPoint p = bb
  1272. .getGeoPointOfRelativePositionWithLinearInterpolation(x
  1273. / viewWidth, y / viewHeight);
  1274. // Log.d(DEBUGTAG,
  1275. // "lon "+p.getLongitudeE6()+" "+xx+" "+asd+" OffsetX = "+OpenStreetMapView.this.mTouchMapOffsetX);
  1276. // Log.d(DEBUGTAG,
  1277. // " "+centerMapTileCoords[0]+" "+tileSizePx+" "+x+" "+upperLeftCornerOfCenterMapTile.x);
  1278. // p.setLongitudeE6(asd);
  1279. // for(int i =0; i<=tileSizePx*(1<<zoomLevel); i++){int Q =
  1280. // Util.x2lon(i, zoomLevel, tileSizePx);Log.d(DEBUGTAG,
  1281. // "lon "+i+" "+Q);}
  1282. return p;
  1283. }
  1284. public GeoPoint fromPixels(float x, float y, double bearing) {
  1285. final int x1 = (int) (x - OpenStreetMapView.this.getWidth() / 2);
  1286. final int y1 = (int) (y - OpenStreetMapView.this.getHeight() / 2);
  1287. final double hypot = Math.hypot(x1, y1);
  1288. final double angle = -1 * Math.signum(y1)
  1289. * Math.toDegrees(Math.acos(x1 / hypot));
  1290. final double angle2 = angle - bearing;
  1291. final int x2 = (int) (Math.cos(Math.toRadians(angle2)) * hypot);
  1292. final int y2 = (int) (Math.sin(Math.toRadians(angle2 - 180)) * hypot);
  1293. return fromPixels(
  1294. (float) (OpenStreetMapView.this.getWidth() / 2 + x2),
  1295. (float) (OpenStreetMapView.this.getHeight() / 2 + y2));
  1296. }
  1297. private static final int EQUATORCIRCUMFENCE = 40075676; // 40075004;
  1298. public float metersToEquatorPixels(final float aMeters) {
  1299. return aMeters
  1300. / EQUATORCIRCUMFENCE
  1301. * OpenStreetMapView.this.mRendererInfo
  1302. .getTileSizePx(OpenStreetMapView.this.mZoomLevel);
  1303. }
  1304. /**
  1305. * Converts a GeoPoint to its ScreenCoordinates. <br/>
  1306. * <br/>
  1307. * <b>CAUTION</b> ! Conversion currently has a large error on
  1308. * <code>zoomLevels <= 7</code>.<br/>
  1309. * The Error on ZoomLevels higher than 7, the error is below
  1310. * <code>1px</code>.<br/>
  1311. * LATER: Add a linear interpolation to minimize this error.
  1312. *
  1313. * <PRE>
  1314. * Zoom Error(m) Error(px)
  1315. * 11 6m 1/12px
  1316. * 10 24m 1/6px
  1317. * 8 384m 1/2px
  1318. * 6 6144m 3px
  1319. * 4 98304m 10px
  1320. * </PRE>
  1321. *
  1322. * @param in
  1323. * the GeoPoint you want the onScreenCoordinates of.
  1324. * @param reuse
  1325. * just pass null if you do not have a Point to be
  1326. * 'recycled'.
  1327. * @return the Point containing the approximated ScreenCoordinates of
  1328. * the GeoPoint passed.
  1329. */
  1330. public Point toPixels(final GeoPoint in, final Point reuse) {
  1331. return toPixels(in, reuse, true);
  1332. }
  1333. public Point tomyPixels(final GeoPoint in, final Point reuse) {
  1334. return tomyPixels(in, reuse, true);
  1335. }
  1336. public Point toPixels(final GeoPoint in, final double bearing,
  1337. final Point reuse) {
  1338. final Point point = toPixels(in, reuse, true);
  1339. final Point out = (reuse != null) ? reuse : new Point();
  1340. final int x1 = point.x - OpenStreetMapView.this.getWidth() / 2;
  1341. final int y1 = point.y - OpenStreetMapView.this.getHeight() / 2;
  1342. final double hypot = Math.hypot(x1, y1);
  1343. final double angle = -1 * Math.signum(y1)
  1344. * Math.toDegrees(Math.acos(x1 / hypot));
  1345. final double angle2 = angle + bearing;
  1346. final int x2 = (int) (Math.cos(Math.toRadians(angle2)) * hypot);
  1347. final int y2 = (int) (Math.sin(Math.toRadians(angle2 - 180)) * hypot);
  1348. out.set(OpenStreetMapView.this.getWidth() / 2 + x2,
  1349. OpenStreetMapView.this.getHeight() / 2 + y2);
  1350. return out;
  1351. }
  1352. protected Point tomyPixels(final GeoPoint in, final Point reuse,
  1353. final boolean doGudermann) {
  1354. final Point out = (reuse != null) ? reuse : new Point();
  1355. final int[] underGeopointTileCoords = Util
  1356. .getMapTileFromCoordinates(in.getLatitudeE6(),
  1357. in.getLongitudeE6(), zoomLevel, null,
  1358. OpenStreetMapView.this.mRendererInfo.PROJECTION);
  1359. /*
  1360. * Calculate the Latitude/Longitude on the left-upper ScreenCoords
  1361. * of the MapTile.
  1362. */
  1363. final BoundingBoxE6 bb = Util.getBoundingBoxFromMapTile(
  1364. underGeopointTileCoords, zoomLevel,
  1365. mRendererInfo.PROJECTION);
  1366. final float[] relativePositionInCenterMapTile;
  1367. if (doGudermann && zoomLevel < 7)
  1368. relativePositionInCenterMapTile = bb
  1369. .getRelativePositionOfGeoPointInBoundingBoxWithExactGudermannInterpolation(
  1370. in.getLatitudeE6(), in.getLongitudeE6(), null);
  1371. else
  1372. relativePositionInCenterMapTile = bb
  1373. .getRelativePositionOfGeoPointInBoundingBoxWithLinearInterpolation(
  1374. in.getLatitudeE6(), in.getLongitudeE6(), null);
  1375. final int tileDiffX = centerMapTileCoords[MAPTILE_LONGITUDE_INDEX]
  1376. - underGeopointTileCoords[MAPTILE_LONGITUDE_INDEX];
  1377. final int tileDiffY = centerMapTileCoords[MAPTILE_LATITUDE_INDEX]
  1378. - underGeopointTileCoords[MAPTILE_LATITUDE_INDEX];
  1379. final int underGeopointTileScreenLeft = upperLeftCornerOfCenterMapTile.x
  1380. - (tileSizePx * tileDiffX);
  1381. final int underGeopointTileScreenTop = upperLeftCornerOfCenterMapTile.y
  1382. - (tileSizePx * tileDiffY);
  1383. final int x = underGeopointTileScreenLeft
  1384. + (int) (relativePositionInCenterMapTile[MAPTILE_LONGITUDE_INDEX] * tileSizePx);
  1385. final int y = underGeopointTileScreenTop
  1386. + (int) (relativePositionInCenterMapTile[MAPTILE_LATITUDE_INDEX] * tileSizePx);
  1387. /* Add up the offset caused by touch. */
  1388. out.set(x, y);
  1389. return out;
  1390. }
  1391. protected Point toPixels(final GeoPoint in, final Point reuse,
  1392. final boolean doGudermann) {
  1393. final Point out = (reuse != null) ? reuse : new Point();
  1394. final int[] underGeopointTileCoords = Util
  1395. .getMapTileFromCoordinates(in.getLatitudeE6(),
  1396. in.getLongitudeE6(), zoomLevel, null,
  1397. OpenStreetMapView.this.mRendererInfo.PROJECTION);
  1398. /*
  1399. * Calculate the Latitude/Longitude on the left-upper ScreenCoords
  1400. * of the MapTile.
  1401. */
  1402. final BoundingBoxE6 bb = Util.getBoundingBoxFromMapTile(
  1403. underGeopointTileCoords, zoomLevel,
  1404. mRendererInfo.PROJECTION);
  1405. final float[] relativePositionInCenterMapTile;
  1406. if (doGudermann && zoomLevel < 7)
  1407. relativePositionInCenterMapTile = bb
  1408. .getRelativePositionOfGeoPointInBoundingBoxWithExactGudermannInterpolation(
  1409. in.getLatitudeE6(), in.getLongitudeE6(), null);
  1410. else
  1411. relativePositionInCenterMapTile = bb
  1412. .getRelativePositionOfGeoPointInBoundingBoxWithLinearInterpolation(
  1413. in.getLatitudeE6(), in.getLongitudeE6(), null);
  1414. final int tileDiffX = centerMapTileCoords[MAPTILE_LONGITUDE_INDEX]
  1415. - underGeopointTileCoords[MAPTILE_LONGITUDE_INDEX];
  1416. final int tileDiffY = centerMapTileCoords[MAPTILE_LATITUDE_INDEX]
  1417. - underGeopointTileCoords[MAPTILE_LATITUDE_INDEX];
  1418. final int underGeopointTileScreenLeft = upperLeftCornerOfCenterMapTile.x
  1419. - (tileSizePx * tileDiffX);
  1420. final int underGeopointTileScreenTop = upperLeftCornerOfCenterMapTile.y
  1421. - (tileSizePx * tileDiffY);
  1422. final int x = underGeopointTileScreenLeft
  1423. + (int) (relativePositionInCenterMapTile[MAPTILE_LONGITUDE_INDEX] * tileSizePx);
  1424. final int y = underGeopointTileScreenTop
  1425. + (int) (relativePositionInCenterMapTile[MAPTILE_LATITUDE_INDEX] * tileSizePx);
  1426. /* Add up the offset caused by touch. */
  1427. out.set(x + OpenStreetMapView.this.mTouchMapOffsetX, y
  1428. + OpenStreetMapView.this.mTouchMapOffsetY);
  1429. return out;
  1430. }
  1431. public Point toPixels2(final GeoPoint in) {
  1432. final Point out = new Point();
  1433. final boolean doGudermann = true;
  1434. final int[] underGeopointTileCoords = Util
  1435. .getMapTileFromCoordinates(in.getLatitudeE6(),
  1436. in.getLongitudeE6(), zoomLevel, null,
  1437. OpenStreetMapView.this.mRendererInfo.PROJECTION);
  1438. /*
  1439. * Calculate the Latitude/Longitude on the left-upper ScreenCoords
  1440. * of the MapTile.
  1441. */
  1442. final BoundingBoxE6 bb = Util.getBoundingBoxFromMapTile(
  1443. underGeopointTileCoords, zoomLevel,
  1444. mRendererInfo.PROJECTION);
  1445. final float[] relativePositionInCenterMapTile;
  1446. if (doGudermann && zoomLevel < 7)
  1447. relativePositionInCenterMapTile = bb
  1448. .getRelativePositionOfGeoPointInBoundingBoxWithExactGudermannInterpolation(
  1449. in.getLatitudeE6(), in.getLongitudeE6(), null);
  1450. else
  1451. relativePositionInCenterMapTile = bb
  1452. .getRelativePositionOfGeoPointInBoundingBoxWithLinearInterpolation(
  1453. in.getLatitudeE6(), in.getLongitudeE6(), null);
  1454. final int tileDiffX = centerMapTileCoords[MAPTILE_LONGITUDE_INDEX]
  1455. - underGeopointTileCoords[MAPTILE_LONGITUDE_INDEX];
  1456. final int tileDiffY = centerMapTileCoords[MAPTILE_LATITUDE_INDEX]
  1457. - underGeopointTileCoords[MAPTILE_LATITUDE_INDEX];
  1458. final int underGeopointTileScreenLeft = upperLeftCornerOfCenterMapTile.x
  1459. - (tileSizePx * tileDiffX);
  1460. final int underGeopointTileScreenTop = upperLeftCornerOfCenterMapTile.y
  1461. - (tileSizePx * tileDiffY);
  1462. final int x = underGeopointTileScreenLeft
  1463. + (int) (relativePositionInCenterMapTile[MAPTILE_LONGITUDE_INDEX] * tileSizePx);
  1464. final int y = underGeopointTileScreenTop
  1465. + (int) (relativePositionInCenterMapTile[MAPTILE_LATITUDE_INDEX] * tileSizePx);
  1466. /* Add up the offset caused by touch. */
  1467. out.set(x, y);
  1468. return out;
  1469. }
  1470. public Path toPixels(final List<GeoPoint> in, final Path reuse) {
  1471. return toPixels(in, reuse, true);
  1472. }
  1473. protected Path toPixels(final List<GeoPoint> in, final Path reuse,
  1474. final boolean doGudermann) throws IllegalArgumentException {
  1475. if (in.size() < 2)
  1476. throw new IllegalArgumentException(
  1477. "List of GeoPoints needs to be at least 2.");
  1478. final Path out = (reuse != null) ? reuse : new Path();
  1479. int i = 0;
  1480. for (GeoPoint gp : in) {
  1481. i++;
  1482. final int[] underGeopointTileCoords = Util
  1483. .getMapTileFromCoordinates(gp.getLatitudeE6(),
  1484. gp.getLongitudeE6(), zoomLevel, null,
  1485. OpenStreetMapView.this.mRendererInfo.PROJECTION);
  1486. /*
  1487. * Calculate the Latitude/Longitude on the left-upper
  1488. * ScreenCoords of the MapTile.
  1489. */
  1490. final BoundingBoxE6 bb = Util.getBoundingBoxFromMapTile(
  1491. underGeopointTileCoords, zoomLevel,
  1492. mRendererInfo.PROJECTION);
  1493. final float[] relativePositionInCenterMapTile;
  1494. if (doGudermann && zoomLevel < 7)
  1495. relativePositionInCenterMapTile = bb
  1496. .getRelativePositionOfGeoPointInBoundingBoxWithExactGudermannInterpolation(
  1497. gp.getLatitudeE6(), gp.getLongitudeE6(),
  1498. null);
  1499. else
  1500. relativePositionInCenterMapTile = bb
  1501. .getRelativePositionOfGeoPointInBoundingBoxWithLinearInterpolation(
  1502. gp.getLatitudeE6(), gp.getLongitudeE6(),
  1503. null);
  1504. final int tileDiffX = centerMapTileCoords[MAPTILE_LONGITUDE_INDEX]
  1505. - underGeopointTileCoords[MAPTILE_LONGITUDE_INDEX];
  1506. final int tileDiffY = centerMapTileCoords[MAPTILE_LATITUDE_INDEX]
  1507. - underGeopointTileCoords[MAPTILE_LATITUDE_INDEX];
  1508. final int underGeopointTileScreenLeft = upperLeftCornerOfCenterMapTile.x
  1509. - (tileSizePx * tileDiffX);
  1510. final int underGeopointTileScreenTop = upperLeftCornerOfCenterMapTile.y
  1511. - (tileSizePx * tileDiffY);
  1512. final int x = underGeopointTileScreenLeft
  1513. + (int) (relativePositionInCenterMapTile[MAPTILE_LONGITUDE_INDEX] * tileSizePx);
  1514. final int y = underGeopointTileScreenTop
  1515. + (int) (relativePositionInCenterMapTile[MAPTILE_LATITUDE_INDEX] * tileSizePx);
  1516. /* Add up the offset caused by touch. */
  1517. if (i == 0)
  1518. out.moveTo(x + OpenStreetMapView.this.mTouchMapOffsetX, y
  1519. + OpenStreetMapView.this.mTouchMapOffsetY);
  1520. else
  1521. out.lineTo(x + OpenStreetMapView.this.mTouchMapOffsetX, y
  1522. + OpenStreetMapView.this.mTouchMapOffsetY);
  1523. }
  1524. return out;
  1525. }
  1526. // addded by cui
  1527. public Point tomy2Pixels(GeoPoint geopoint) {
  1528. Point p = new Point();
  1529. final int[] underGeopointTileCoords = Util
  1530. .getMapTileFromCoordinates(geopoint.getLatitudeE6(),
  1531. geopoint.getLongitudeE6(), zoomLevel, null,
  1532. OpenStreetMapView.this.mRendererInfo.PROJECTION);
  1533. /*
  1534. * Calculate the Latitude/Longitude on the left-upper ScreenCoords
  1535. * of the MapTile.
  1536. */
  1537. final BoundingBoxE6 bb = Util.getBoundingBoxFromMapTile(
  1538. underGeopointTileCoords, zoomLevel,
  1539. mRendererInfo.PROJECTION);
  1540. final float[] relativePositionInCenterMapTile;
  1541. if (zoomLevel < 7)
  1542. relativePositionInCenterMapTile = bb
  1543. .getRelativePositionOfGeoPointInBoundingBoxWithExactGudermannInterpolation(
  1544. geopoint.getLatitudeE6(),
  1545. geopoint.getLongitudeE6(), null);
  1546. else
  1547. relativePositionInCenterMapTile = bb
  1548. .getRelativePositionOfGeoPointInBoundingBoxWithLinearInterpolation(
  1549. geopoint.getLatitudeE6(),
  1550. geopoint.getLongitudeE6(), null);
  1551. final int tileDiffX = centerMapTileCoords[MAPTILE_LONGITUDE_INDEX]
  1552. - underGeopointTileCoords[MAPTILE_LONGITUDE_INDEX];
  1553. final int tileDiffY = centerMapTileCoords[MAPTILE_LATITUDE_INDEX]
  1554. - underGeopointTileCoords[MAPTILE_LATITUDE_INDEX];
  1555. final int underGeopointTileScreenLeft = upperLeftCornerOfCenterMapTile.x
  1556. - (tileSizePx * tileDiffX);
  1557. final int underGeopointTileScreenTop = upperLeftCornerOfCenterMapTile.y
  1558. - (tileSizePx * tileDiffY);
  1559. final int x = underGeopointTileScreenLeft
  1560. + (int) (relativePositionInCenterMapTile[MAPTILE_LONGITUDE_INDEX] * tileSizePx);
  1561. final int y = underGeopointTileScreenTop
  1562. + (int) (relativePositionInCenterMapTile[MAPTILE_LATITUDE_INDEX] * tileSizePx);
  1563. p.x = x;
  1564. p.y = y;
  1565. return p;
  1566. }
  1567. // end
  1568. public Path toPixelsTrackPoints(List<GeoPoint> in, Point baseCoord,
  1569. GeoPoint baseLocation) throws IllegalArgumentException {
  1570. if (in.size() < 2)
  1571. return null;
  1572. // throw new
  1573. // IllegalArgumentException("List of GeoPoints needs to be at least 2.");
  1574. final Path out = new Path();
  1575. final boolean doGudermann = true;
  1576. int i = 0;
  1577. int lastX = 0, lastY = 0;
  1578. for (GeoPoint tp : in) {
  1579. final int[] underGeopointTileCoords = Util
  1580. .getMapTileFromCoordinates(tp.getLatitudeE6(),
  1581. tp.getLongitudeE6(), zoomLevel, null,
  1582. OpenStreetMapView.this.mRendererInfo.PROJECTION);
  1583. /*
  1584. * Calculate the Latitude/Longitude on the left-upper
  1585. * ScreenCoords of the MapTile.
  1586. */
  1587. final BoundingBoxE6 bb = Util.getBoundingBoxFromMapTile(
  1588. underGeopointTileCoords, zoomLevel,
  1589. mRendererInfo.PROJECTION);
  1590. final float[] relativePositionInCenterMapTile;
  1591. if (doGudermann && zoomLevel < 7)
  1592. relativePositionInCenterMapTile = bb
  1593. .getRelativePositionOfGeoPointInBoundingBoxWithExactGudermannInterpolation(
  1594. tp.getLatitudeE6(), tp.getLongitudeE6(),
  1595. null);
  1596. else
  1597. relativePositionInCenterMapTile = bb
  1598. .getRelativePositionOfGeoPointInBoundingBoxWithLinearInterpolation(
  1599. tp.getLatitudeE6(), tp.getLongitudeE6(),
  1600. null);
  1601. final int tileDiffX = centerMapTileCoords[MAPTILE_LONGITUDE_INDEX]
  1602. - underGeopointTileCoords[MAPTILE_LONGITUDE_INDEX];
  1603. final int tileDiffY = centerMapTileCoords[MAPTILE_LATITUDE_INDEX]
  1604. - underGeopointTileCoords[MAPTILE_LATITUDE_INDEX];
  1605. final int underGeopointTileScreenLeft = upperLeftCornerOfCenterMapTile.x
  1606. - (tileSizePx * tileDiffX);
  1607. final int underGeopointTileScreenTop = upperLeftCornerOfCenterMapTile.y
  1608. - (tileSizePx * tileDiffY);
  1609. final int x = underGeopointTileScreenLeft
  1610. + (int) (relativePositionInCenterMapTile[MAPTILE_LONGITUDE_INDEX] * tileSizePx);
  1611. final int y = underGeopointTileScreenTop
  1612. + (int) (relativePositionInCenterMapTile[MAPTILE_LATITUDE_INDEX] * tileSizePx);
  1613. /* Add up the offset caused by touch. */
  1614. if (i == 0) {
  1615. out.setLastPoint(x, y);
  1616. lastX = x;
  1617. lastY = y;
  1618. baseCoord.x = x;
  1619. baseCoord.y = y;
  1620. baseLocation.setCoordsE6(tp.getLatitudeE6(),
  1621. tp.getLongitudeE6());
  1622. i++;
  1623. } else {
  1624. if (Math.abs(lastX - x) > 5 || Math.abs(lastY - y) > 5) {
  1625. out.lineTo(x, y);
  1626. lastX = x;
  1627. lastY = y;
  1628. i++;
  1629. }
  1630. }
  1631. }
  1632. return out;
  1633. }
  1634. // added by cui 2013 3 19
  1635. public Path toPixelsDrawLine(List<GeoPoint> in, Point baseCoord,
  1636. GeoPoint baseLocation) throws IllegalArgumentException {
  1637. if (in.size() < 2)
  1638. return null;
  1639. final Path out = new Path();
  1640. final boolean doGudermann = true;
  1641. int i = 0;
  1642. int lastX = 0, lastY = 0;
  1643. for (GeoPoint tp : in) {
  1644. Log.i("DrawOverlay1", "in.size()==" + in.size());
  1645. final int[] underGeopointTileCoords = Util
  1646. .getMapTileFromCoordinates(tp.getLatitudeE6(),
  1647. tp.getLongitudeE6(), zoomLevel, null,
  1648. OpenStreetMapView.this.mRendererInfo.PROJECTION);
  1649. /*
  1650. * Calculate the Latitude/Longitude on the left-upper
  1651. * ScreenCoords of the MapTile.
  1652. */
  1653. final BoundingBoxE6 bb = Util.getBoundingBoxFromMapTile(
  1654. underGeopointTileCoords, zoomLevel,
  1655. mRendererInfo.PROJECTION);
  1656. final float[] relativePositionInCenterMapTile;
  1657. if (doGudermann && zoomLevel < 7)
  1658. relativePositionInCenterMapTile = bb
  1659. .getRelativePositionOfGeoPointInBoundingBoxWithExactGudermannInterpolation(
  1660. tp.getLatitudeE6(), tp.getLongitudeE6(),
  1661. null);
  1662. else
  1663. relativePositionInCenterMapTile = bb
  1664. .getRelativePositionOfGeoPointInBoundingBoxWithLinearInterpolation(
  1665. tp.getLatitudeE6(), tp.getLongitudeE6(),
  1666. null);
  1667. final int tileDiffX = centerMapTileCoords[MAPTILE_LONGITUDE_INDEX]
  1668. - underGeopointTileCoords[MAPTILE_LONGITUDE_INDEX];
  1669. final int tileDiffY = centerMapTileCoords[MAPTILE_LATITUDE_INDEX]
  1670. - underGeopointTileCoords[MAPTILE_LATITUDE_INDEX];
  1671. final int underGeopointTileScreenLeft = upperLeftCornerOfCenterMapTile.x
  1672. - (tileSizePx * tileDiffX);
  1673. final int underGeopointTileScreenTop = upperLeftCornerOfCenterMapTile.y
  1674. - (tileSizePx * tileDiffY);
  1675. final int x = underGeopointTileScreenLeft
  1676. + (int) (relativePositionInCenterMapTile[MAPTILE_LONGITUDE_INDEX] * tileSizePx);
  1677. final int y = underGeopointTileScreenTop
  1678. + (int) (relativePositionInCenterMapTile[MAPTILE_LATITUDE_INDEX] * tileSizePx);
  1679. /* Add up the offset caused by touch. */
  1680. if (i == 0) {
  1681. out.setLastPoint(x, y);
  1682. lastX = x;
  1683. lastY = y;
  1684. baseCoord.x = x;
  1685. baseCoord.y = y;
  1686. baseLocation.setCoordsE6(tp.getLatitudeE6(),
  1687. tp.getLongitudeE6());
  1688. i++;
  1689. } else {
  1690. if (Math.abs(lastX - x) > 5 || Math.abs(lastY - y) > 5) {
  1691. out.lineTo(x, y);
  1692. lastX = x;
  1693. lastY = y;
  1694. i++;
  1695. }
  1696. }
  1697. }
  1698. return out;
  1699. }
  1700. // added by cui 2013 3 21
  1701. public Point toPointfromGeoPoint(GeoPoint tp)
  1702. throws IllegalArgumentException {
  1703. Point p = new Point();
  1704. final int[] underGeopointTileCoords = Util
  1705. .getMapTileFromCoordinates(tp.getLatitudeE6(),
  1706. tp.getLongitudeE6(), zoomLevel, null,
  1707. OpenStreetMapView.this.mRendererInfo.PROJECTION);
  1708. /*
  1709. * Calculate the Latitude/Longitude on the left-upper ScreenCoords
  1710. * of the MapTile.
  1711. */
  1712. final BoundingBoxE6 bb = Util.getBoundingBoxFromMapTile(
  1713. underGeopointTileCoords, zoomLevel,
  1714. mRendererInfo.PROJECTION);
  1715. final float[] relativePositionInCenterMapTile;
  1716. if (zoomLevel < 7)
  1717. relativePositionInCenterMapTile = bb
  1718. .getRelativePositionOfGeoPointInBoundingBoxWithExactGudermannInterpolation(
  1719. tp.getLatitudeE6(), tp.getLongitudeE6(), null);
  1720. else
  1721. relativePositionInCenterMapTile = bb
  1722. .getRelativePositionOfGeoPointInBoundingBoxWithLinearInterpolation(
  1723. tp.getLatitudeE6(), tp.getLongitudeE6(), null);
  1724. final int tileDiffX = centerMapTileCoords[MAPTILE_LONGITUDE_INDEX]
  1725. - underGeopointTileCoords[MAPTILE_LONGITUDE_INDEX];
  1726. final int tileDiffY = centerMapTileCoords[MAPTILE_LATITUDE_INDEX]
  1727. - underGeopointTileCoords[MAPTILE_LATITUDE_INDEX];
  1728. final int underGeopointTileScreenLeft = upperLeftCornerOfCenterMapTile.x
  1729. - (tileSizePx * tileDiffX);
  1730. final int underGeopointTileScreenTop = upperLeftCornerOfCenterMapTile.y
  1731. - (tileSizePx * tileDiffY);
  1732. final int x = underGeopointTileScreenLeft
  1733. + (int) (relativePositionInCenterMapTile[MAPTILE_LONGITUDE_INDEX] * tileSizePx);
  1734. final int y = underGeopointTileScreenTop
  1735. + (int) (relativePositionInCenterMapTile[MAPTILE_LATITUDE_INDEX] * tileSizePx);
  1736. /* Add up the offset caused by touch. */
  1737. p.x = x;
  1738. p.y = y;
  1739. return p;
  1740. }
  1741. }
  1742. private class SimpleInvalidationHandler extends Handler {
  1743. @Override
  1744. public void handleMessage(final Message msg) {
  1745. switch (msg.what) {
  1746. case OpenStreetMapTileDownloader.MAPTILEDOWNLOADER_SUCCESS_ID:
  1747. case OpenStreetMapTileFilesystemProvider.MAPTILEFSLOADER_SUCCESS_ID:
  1748. OpenStreetMapView.this.invalidate();
  1749. break;
  1750. case OpenStreetMapTileFilesystemProvider.ERROR_MESSAGE:
  1751. Message.obtain(mMainActivityCallbackHandler,
  1752. OpenStreetMapTileFilesystemProvider.ERROR_MESSAGE,
  1753. msg.obj).sendToTarget();
  1754. break;
  1755. case OpenStreetMapTileFilesystemProvider.INDEXIND_SUCCESS_ID:
  1756. if (mZoomLevel > mRendererInfo.ZOOM_MAXLEVEL)
  1757. mZoomLevel = mRendererInfo.ZOOM_MAXLEVEL;
  1758. if (mZoomLevel < mRendererInfo.ZOOM_MINLEVEL)
  1759. mZoomLevel = mRendererInfo.ZOOM_MINLEVEL;
  1760. Message.obtain(mMainActivityCallbackHandler, OpenStreetMapTileFilesystemProvider.MAP_ACTIVITY_SETTITLE)
  1761. .sendToTarget();
  1762. OpenStreetMapView.this.invalidate();
  1763. break;
  1764. case OpenStreetMapTileFilesystemProvider.MAP_LONLAT_ID:
  1765. Bundle bundle = msg.getData();
  1766. int maxx = bundle.getInt("maxx");
  1767. int minx = bundle.getInt("minx");
  1768. int maxy = bundle.getInt("maxy");
  1769. int miny = bundle.getInt("miny");
  1770. boolean isfirst = bundle.getBoolean("isfirst");
  1771. double lon_max = Util.tile2lon(maxx, 17);
  1772. double lon_min = Util.tile2lon(minx, 17);
  1773. double lat_min = Util.tile2lat(maxy, 17, 1);
  1774. double lat_max = Util.tile2lat(miny, 17, 1);
  1775. d_lat = (lat_max + lat_min) / 2;
  1776. d_lon = (lon_max + lon_min) / 2;
  1777. Log.i("CUITEST", "d_lon=" + d_lon + " d_lat=" + d_lat);
  1778. if (mapflag)
  1779. if ((lon_min > last_max_lon && lat_min > last_max_lat)
  1780. || (lon_max < last_min_lon && lat_max < last_min_lat)) {
  1781. if (isfirst) {
  1782. setZoomLevel(18);
  1783. Log.i("CUITEST", "isfirst=" + isfirst);
  1784. }
  1785. tomap();
  1786. mapflag = false;
  1787. }
  1788. last_max_lon = lon_max;
  1789. last_min_lon = lon_min;
  1790. last_max_lat = lat_max;
  1791. last_min_lat = lat_min;
  1792. break;
  1793. }
  1794. }
  1795. }
  1796. private class OpenStreetMapViewGestureDetectorListener implements
  1797. OnGestureListener, OnDoubleTapListener {
  1798. // @Override
  1799. public boolean onDown(MotionEvent e) {
  1800. return false;
  1801. }
  1802. // @Override
  1803. public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
  1804. float velocityY) {
  1805. // LATER Could be used for smoothly 'scroll-out' the map on a fast
  1806. // motion.
  1807. return false;
  1808. }
  1809. // @Override
  1810. public void onLongPress(MotionEvent e) {
  1811. OpenStreetMapView.this.onLongPress(e);
  1812. }
  1813. // @Override
  1814. public boolean onScroll(MotionEvent e1, MotionEvent e2,
  1815. float distanceX, float distanceY) {
  1816. return false;
  1817. }
  1818. // @Override
  1819. public void onShowPress(MotionEvent e) {
  1820. }
  1821. // @Override
  1822. public boolean onSingleTapUp(MotionEvent e) {
  1823. return OpenStreetMapView.this.onSingleTapUp(e);
  1824. }
  1825. public boolean onDoubleTap(MotionEvent e) {
  1826. return OpenStreetMapView.this.onDoubleTap(e);
  1827. }
  1828. public boolean onDoubleTapEvent(MotionEvent e) {
  1829. // Auto-generated method stub
  1830. return false;
  1831. }
  1832. public boolean onSingleTapConfirmed(MotionEvent e) {
  1833. // Auto-generated method stub
  1834. return false;
  1835. }
  1836. }
  1837. public void setMainActivityCallbackHandler(Handler callbackHandler) {
  1838. this.mMainActivityCallbackHandler = callbackHandler;
  1839. }
  1840. public void freeDatabases() {
  1841. mTileProvider.freeDatabases();
  1842. }
  1843. public void tomap() {
  1844. // TODO Auto-generated method stub
  1845. if (d_lon != 0 && d_lat != 0) {
  1846. GeoPoint p = GeoPoint.from2DoubleString(String.valueOf(d_lat),
  1847. String.valueOf(d_lon));
  1848. if (p != null) {
  1849. this.getController()
  1850. .animateTo(
  1851. p,
  1852. OpenStreetMapViewController.AnimationType.MIDDLEPEAKSPEED,
  1853. OpenStreetMapViewController.ANIMATION_SMOOTHNESS_HIGH,
  1854. OpenStreetMapViewController.ANIMATION_DURATION_DEFAULT);
  1855. this.invalidate();
  1856. }
  1857. }
  1858. }
  1859. public void setMapFlag(boolean mapflag){
  1860. this.mapflag=mapflag;
  1861. }
  1862. }