<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          自學HarmonyOS應用開發(fā)(51)- 獲取瓦片地圖數據(續(xù))

          共 3448字,需瀏覽 7分鐘

           ·

          2021-06-28 14:28

          計算瓦片位置

          根據前一篇文章中說明的計算方法,我們提供以下代碼實現:

          //https://wiki.openstreetmap.org/wiki/Slippy_map_tilenamesint getTileCol(double long_deg, int zoom){    int total_cols = (int)Math.pow(2, zoom);    return (int)((long_deg + 180)/360 * total_cols);}
          int getTileRow(double lat_deg, int zoom){ double tan = Math.tan(Math.toRadians(lat_deg)); double asinh = Math.log(tan + Math.sqrt(tan * tan + 1)); return (int)((1.0 - asinh / Math.PI) * Math.pow(2, zoom - 1));}


          獲取瓦片數據

          以下是獲取瓦片數據的代碼。由于整個獲取過程的時間無法確定,因此將獲取動作放入另外的任務。獲取任務結束之后再觸發(fā)UI線程的更新動作。

          public void loadMapTile(){    getContext().getGlobalTaskDispatcher(TaskPriority.DEFAULT).asyncDispatch(new Runnable() {        @Override        public void run() {            int tileCol = getTileCol(longtitude, zoom);            int tileRow = getTileRow(latitude, zoom);            String urlString = String.format(getMapUrlString(), tileCol, tileRow, zoom);            PixelMap map = getImagePixmap(urlString);            tile = new PixelMapHolder(map);            getContext().getUITaskDispatcher().asyncDispatch(new Runnable() {                @Override                public void run() {                    TileMap.this.invalidate();                }            });        }    });}


          生成數據獲取URL

          代碼中用到對的getMapUrlString的實現如下:

          public final String getMapUrlString(){    // 高德地圖 - 矢量    final String GAODE_V_MAP_URL = "https://webrd02.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=2&style=8&x=%d&y=%d&z=%d";    // 高德地圖 - 道路    final String GAODE_R_MAP_URL = "https://webst02.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=2&style=8&x=%d&y=%d&z=%d";    // 高德地圖 - 衛(wèi)星    final String GAODE_S_MAP_URL = "https://webst01.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=2&style=6&x=%d&y=%d&z=%d";    switch(mapSource){        case GAODE_VECTOR:            return GAODE_V_MAP_URL;        case GAODE_ROAD:            return GAODE_R_MAP_URL;        case GAODE_SATELLITE:            return GAODE_S_MAP_URL;        default:            return null;    }}


          通過網絡獲取瓦片位圖

          getImagePixmap的代碼原封不動引自董昱老師的TinyMap

          public static PixelMap getImagePixmap(String urlString) {    try {        URL url = new URL(urlString);        URLConnection con = url.openConnection();        con.setConnectTimeout(500*1000);        InputStream is = con.getInputStream();        ImageSource source = ImageSource.create(is, new ImageSource.SourceOptions());        ImageSource.DecodingOptions options = new ImageSource.DecodingOptions();        options.desiredSize = new Size(513,513);        PixelMap pixelMap = source.createPixelmap(options);        is.close();        return pixelMap;    } catch (Exception e) {        return null;    }}

          動作視頻


          參考代碼

          完整代碼可以從以下鏈接下載:

          https://github.com/xueweiguo/Harmony/tree/master/StopWatch


          參考資料

          Slippy map tilenames(包含各種轉換示例代碼):

          https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames\

          董昱老師的TinyMap:

          https://gitee.com/dongyu1009/tiny-map-for-harmony-os/tree/master/tinymap


          作者著作介紹

          《實戰(zhàn)Python設計模式》是作者去年3月份出版的技術書籍,該書利用Python 的標準GUI 工具包tkinter,通過可執(zhí)行的示例對23 個設計模式逐個進行說明。這樣一方面可以使讀者了解真實的軟件開發(fā)工作中每個設計模式的運用場景和想要解決的問題;另一方面通過對這些問題的解決過程進行說明,讓讀者明白在編寫代碼時如何判斷使用設計模式的利弊,并合理運用設計模式。

          對設計模式感興趣而且希望隨學隨用的讀者通過本書可以快速跨越從理解到運用的門檻;希望學習Python GUI 編程的讀者可以將本書中的示例作為設計和開發(fā)的參考;使用Python 語言進行圖像分析、數據處理工作的讀者可以直接以本書中的示例為基礎,迅速構建自己的系統(tǒng)架構。




          覺得本文有幫助?請分享給更多人。

          關注微信公眾號【面向對象思考】輕松學習每一天!

          面向對象開發(fā),面向對象思考!



          瀏覽 82
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  国产二区三区 | 久爱视颊在线观看 | 首页国产中文字幕 | 欧美人成人无码 | 少年阿宾麻豆传媒 |