自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

卡片服務(wù)開(kāi)發(fā)之如何開(kāi)發(fā)一個(gè)地圖服務(wù)卡片

開(kāi)發(fā) 前端
卡片現(xiàn)有支持的基礎(chǔ)組件有:button、calendar、chart、clock、divider、image、input、progress、span、text可以看到現(xiàn)有的卡片組件并不支持地圖的開(kāi)發(fā),那么如何在卡片上顯示地圖尼?

[[413808]]

想了解更多內(nèi)容,請(qǐng)?jiān)L問(wèn):

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.51cto.com

前言

處于隱私保護(hù)借用熊貓基地定位,代碼層實(shí)現(xiàn)了獲取實(shí)時(shí)定位功能。

效果圖

卡片效果2x2+2x44x4縮放

關(guān)鍵技術(shù)及實(shí)現(xiàn)原理

卡片現(xiàn)有支持的基礎(chǔ)組件有:button、calendar、chart、clock、divider、image、input、progress、span、text

可以看到現(xiàn)有的卡片組件并不支持地圖的開(kāi)發(fā),那么如何在卡片上顯示地圖尼?

通過(guò)image組件+高德地圖WebAPI的靜態(tài)地圖即可實(shí)現(xiàn)地圖的顯示。

-以上方便有開(kāi)發(fā)卡片經(jīng)驗(yàn)的開(kāi)發(fā)者提供思路,具體方式方法如下

從零開(kāi)始

創(chuàng)建項(xiàng)目

打開(kāi)DevEco Studio工具,點(diǎn)擊File->New->New Project創(chuàng)建一個(gè)Empty Ability(JS)如下圖:

SDK選用API 5

創(chuàng)建后的結(jié)構(gòu):

卡片服務(wù)開(kāi)發(fā)之如何開(kāi)發(fā)一個(gè)地圖服務(wù)卡片-鴻蒙HarmonyOS技術(shù)社區(qū)

首先修改程序的配置文件:

打開(kāi)config.json,修改卡片支持類型情況:

卡片服務(wù)開(kāi)發(fā)之如何開(kāi)發(fā)一個(gè)地圖服務(wù)卡片-鴻蒙HarmonyOS技術(shù)社區(qū)

添加權(quán)限:

卡片服務(wù)開(kāi)發(fā)之如何開(kāi)發(fā)一個(gè)地圖服務(wù)卡片-鴻蒙HarmonyOS技術(shù)社區(qū)

配置完成還需要在MainAbility中顯示的申明使用權(quán)限信息,詳情參考文檔配置相關(guān)內(nèi)容:

打開(kāi)MainAbility添加方法:

  1. //獲取權(quán)限 
  2.    private void requestPermission() { 
  3.        String[] permission = { 
  4.                "ohos.permission.LOCATION"
  5.                "ohos.permission.LOCATION_IN_BACKGROUND"
  6.        }; 
  7.        List<String> applyPermissions = new ArrayList<>(); 
  8.        for (String element : permission) { 
  9.            if (verifySelfPermission(element) != 0) { 
  10.                if (canRequestPermission(element)) { 
  11.                    applyPermissions.add(element); 
  12.                } 
  13.            } 
  14.        } 
  15.        requestPermissionsFromUser(applyPermissions.toArray(new String[0]), 0); 
  16.    } 

并在onStart方法中調(diào)用requestPermission();方法。

修改界面:

打開(kāi)widget下的pages/index/imdex.hml

  1. <div class="container"
  2.         <div class="container-inner" > 
  3.             <div class="container-img"
  4.                 <stack> 
  5.                     <image src="{{imgSrc}}" class="bg-img"></image> 
  6.                     <div class="container-show-text" > 
  7.                         <text class="show-text" >當(dāng)前檢索項(xiàng):</text> 
  8.                         <text class="show-text" style="color: coral;"  >{{searchText}}</text> 
  9.                     </div> 
  10.                      
  11.                     <div class="container-map-ctl"
  12.                         <button class="map-ctl-btn" @click="mapAddEvent"  type="circle">+</button> 
  13.                         <button class="map-ctl-btn" @click="mapReduceEvent"  type="circle">-</button> 
  14.                     </div> 
  15.                     <div show="{{showCtlButton}}" class="container-ctl" > 
  16.                         <button class="ctl-btn" @click="searchCheckedEvent0">{{searchBtns[0]}}</button> 
  17.                         <button class="ctl-btn" @click="searchCheckedEvent1">{{searchBtns[1]}}</button> 
  18.                         <button class="ctl-btn" @click="searchCheckedEvent2">{{searchBtns[2]}}</button> 
  19.                         <button class="ctl-btn" @click="searchCheckedEvent3">{{searchBtns[3]}}</button> 
  20.                         <button class="ctl-btn" @click="searchCheckedEvent4">{{searchBtns[4]}}</button> 
  21.                     </div> 
  22.                 </stack> 
  23.             </div> 
  24.         </div> 
  25. </div> 

需要注意:卡片的事件不能使用表達(dá)式,不能使用for語(yǔ)句循環(huán)構(gòu)建

樣式調(diào)整文件pages/index/imdex.css:

  1. .container { 
  2.     flex-direction: column
  3.     justify-content: center; 
  4.     align-items: center; 
  5.  
  6. .bg-img { 
  7.     flex-shrink: 0; 
  8.     height: 100%; 
  9.     object-fit: cover; 
  10.  
  11. .container-ctl{ 
  12.     opacity: 0.9; 
  13.     width: 100%; 
  14.     height: 100%; 
  15.     justify-content: center; 
  16.     flex-direction: row; 
  17.     align-items: flex-end
  18.     bottom: 3px; 
  19. .ctl-btn{ 
  20.     padding: 3px 6px; 
  21.     margin:3px 6px; 
  22.     font-size: 12px; 
  23.     border-radius: 3px; 
  24.     background-color: #409eff; 
  25.     border: 1px solid #cbcbcb; 
  26.     box-shadow: 1px 1px 3px #a8a8a8; 
  27. .container-map-ctl{ 
  28.     opacity: 0.8; 
  29.     justify-content: flex-end
  30.     margin-right: 3px; 
  31. .map-ctl-btn{ 
  32.     background-color: #409eff; 
  33.     border: 1px solid #cbcbcb; 
  34.     box-shadow: 1px 1px 3px #a8a8a8; 
  35.     width: 24px; 
  36.     height: 24px; 
  37.     margin:3px; 
  38.  
  39. .container-show-text{ 
  40.     padding: 9px; 
  41. .show-text{ 
  42.     font-size: 8px; 
  43.     font-weight: bolder; 
  44.  

json配置信息修改pages/index/index.json:

  1.   "data": { 
  2.     "showCtlButton"false,//是否顯示button。由Java傳值且在2x2的界面不顯示 
  3.     "imgSrc""/common/ic_default_image@3x.png",//默認(rèn)圖片 
  4.     "searchText"""
  5.     "searchBtns": []//配置的button按鈕信息 
  6.   }, 
  7.   "actions": { 
  8.     "searchCheckedEvent0": { 
  9.       "action""message"
  10.       "params": { 
  11.         "index": 0, 
  12.         "name""checkSearch" 
  13.       } 
  14.     }, 
  15.     "searchCheckedEvent1": { 
  16.       "action""message"
  17.       "params": { 
  18.         "index": 1, 
  19.         "name""checkSearch" 
  20.       } 
  21.     }, 
  22.     "searchCheckedEvent2": { 
  23.       "action""message"
  24.       "params": { 
  25.         "index": 2, 
  26.         "name""checkSearch" 
  27.       } 
  28.     }, 
  29.     "searchCheckedEvent3": { 
  30.       "action""message"
  31.       "params": { 
  32.         "index": 3, 
  33.         "name""checkSearch" 
  34.       } 
  35.     }, 
  36.     "searchCheckedEvent4": { 
  37.       "action""message"
  38.       "params": { 
  39.         "index": 4, 
  40.         "name""checkSearch" 
  41.       } 
  42.     }, 
  43.     "mapAddEvent": { 
  44.       "action""message"
  45.       "params": { 
  46.         "name""mapAdd" 
  47.       } 
  48.     }, 
  49.     "mapReduceEvent": { 
  50.       "action""message"
  51.       "params": { 
  52.         "name""mapReduce" 
  53.       } 
  54.     } 
  55.   } 

后臺(tái)邏輯

由于更新卡片時(shí)需要提供formId,我們對(duì)FormController及FormControllerManager這兩個(gè)幫助類進(jìn)行一個(gè)修改。

打開(kāi)java目錄下的FormController文件并添加受保護(hù)的屬性 formId,并修改構(gòu)造函數(shù)。

然后進(jìn)入FormControllerManager找到createFormController、getController、newInstance進(jìn)行修改。

createFormController:

在newInstance方法中添加參數(shù)formId,如下圖:

getController:

在newInstance方法中添加參數(shù)formId,如下圖:

newInstace:

該方法是動(dòng)態(tài)的創(chuàng)建WidgetImpl方法,類似于IOC作用。

找到j(luò)ava目錄下的widget/widget/widgetImpl,卡片的所有邏輯都在該文件內(nèi)

首先修改構(gòu)造函數(shù)及定義基礎(chǔ)屬性等

因上述修改了FormController及FormControllerManager構(gòu)造函數(shù)必須增加Long formId參數(shù)

  1. private static Location slocation=null;//當(dāng)前位置信息 
  2.    private Boolean slocationChanged=false;//位置是否修改 
  3.    private  int dimension=2;//當(dāng)前卡片模式  2x2=2;2x4=3;4x4=4; 
  4.    private List<String> defualtBtn=new ArrayList<>();//界面下方的按鈕列表 
  5.    private static Locator locator=null;//坐標(biāo)獲取類 
  6.    private LocatorCallBack locatorCallBack=new LocatorCallBack();//坐標(biāo)獲取后返回調(diào)用類 
  7.    private int mRoom=16;//靜態(tài)地圖顯示層級(jí) 
  8.    private String markType="";//靜態(tài)地圖周邊搜索關(guān)鍵字 
  9.    private String mSize="500*500";//靜態(tài)地圖大小 
  10.    private List<String> mKeyLocation=new ArrayList<>();//靜態(tài)地圖獲取周邊標(biāo)記的坐標(biāo) 
  11.    RequestParam requestParam = new RequestParam(RequestParam.PRIORITY_ACCURACY, 20, 0); 
  12.  
  13.  
  14.    public WidgetImpl(Context context, String formName, Integer dimension,Long formId) { 
  15.        super(context, formName, dimension,formId); 
  16.        this.dimension=dimension; 
  17.        //獲取當(dāng)前定位 
  18.        if(locator==null){ 
  19.            locator=new Locator(context); 
  20.            locator.startLocating(requestParam,locatorCallBack); 
  21.        } 
  22.        switch (dimension){ 
  23.            case 2:{ 
  24.                mSize="300*300"
  25.                mRoom=13; 
  26.                break; 
  27.            } 
  28.            case 3:{ 
  29.              mSize="500*250"
  30.              mRoom=13; 
  31.              break; 
  32.            } 
  33.            case 4:{ 
  34.                mSize="500*500"
  35.                mRoom=15; 
  36.                break; 
  37.            } 
  38.        } 
  39.    } 
  40.  
  41.    public class LocatorCallBack implements LocatorCallback{ 
  42.  
  43.        @Override 
  44.        public void onLocationReport(Location location) { 
  45.            slocation=location; 
  46.            //周邊信息接口額度有限,限制為當(dāng)坐標(biāo)改變時(shí)刷新坐標(biāo)mark信息,并更新卡片 
  47.            if(location==slocation || slocation==null
  48.                return
  49.            refreshMark(); 
  50.            updateFormData(formId); 
  51.        } 
  52.  
  53.        @Override 
  54.        public void onStatusChanged(int i) { 
  55.  
  56.        } 
  57.  
  58.        @Override 
  59.        public void onErrorReport(int i) { 
  60.  
  61.        } 
  62.    } 

修改createFormController,該方法在卡片創(chuàng)建時(shí)調(diào)用,我們需要把頁(yè)面需要的參數(shù)傳遞過(guò)去

注意網(wǎng)絡(luò)圖片需要使用“通過(guò)內(nèi)存圖片方式使用image組件”

  1. @Override 
  2.    public ProviderFormInfo bindFormData(){ 
  3.        defualtBtn=new ArrayList<>(); 
  4.        defualtBtn.add("酒店"); 
  5.        defualtBtn.add("餐飲"); 
  6.        defualtBtn.add("景點(diǎn)"); 
  7.        defualtBtn.add("加油站"); 
  8.        if(defualtBtn.size()<5){ 
  9.            for(int i=defualtBtn.size();i<5;i++){ 
  10.                defualtBtn.add("未設(shè)置"); 
  11.            } 
  12.        } 
  13.        this.markType=defualtBtn.get(0); 
  14.        this.refreshMark(); 
  15.        FormBindingData formBindingData=null
  16.        ZSONObject zsonObject =new ZSONObject(); 
  17.        zsonObject.put("imgSrc","memory://amap.png"); 
  18.        zsonObject.put("showCtlButton",this.dimension!=2); 
  19.        zsonObject.put("searchBtns",defualtBtn); 
  20.        zsonObject.put("searchText",markType); 
  21.        formBindingData=new FormBindingData(zsonObject); 
  22.        ProviderFormInfo formInfo = new ProviderFormInfo(); 
  23.        formInfo.setJsBindingData(formBindingData); 
  24.        String amapUrl=getMapImageUrl(mKeyLocation); 
  25.        byte[] bytes= HttpImageUtils.doGetRequestForFile(amapUrl); 
  26.        formBindingData.addImageData("amap.png",bytes); 
  27.        return formInfo; 
  28.    } 

初始化卡片后改進(jìn)onTriggerFormEvent

該方法為接收卡片事件,message為事件傳遞的params參數(shù)

  1. @Override 
  2.   public void onTriggerFormEvent(long formId, String message) { 
  3.       ZSONObject request=ZSONObject.stringToZSON(message); 
  4.       String EventName=request.getString("name"); 
  5.       switch (EventName){ 
  6.           case "checkSearch":{ 
  7.               int index=request.getIntValue("index"); 
  8.               markType=defualtBtn.get(index); 
  9.               this.refreshMark(); 
  10.               break; 
  11.           } 
  12.           case "mapAdd":{ 
  13.               if(mRoom<17){ 
  14.                   mRoom+=1; 
  15.               } 
  16.               break; 
  17.           } 
  18.           case "mapReduce":{ 
  19.               if(mRoom>0){ 
  20.                   mRoom-=1; 
  21.               } 
  22.               break; 
  23.           } 
  24.       } 
  25.       updateFormData(formId); 
  26.   } 

修改更新卡片信息的方法(此方法不僅是系統(tǒng)會(huì)定時(shí)刷新,也有主動(dòng)刷新的調(diào)用如:卡片事件改變后調(diào)用,坐標(biāo)改變后的調(diào)用,這也是需要修改FormController、FormControllerManager增加formId屬性的原因,因?yàn)樵谥鲃?dòng)刷新時(shí)需要formId參數(shù))

此處還有一個(gè)重點(diǎn)就是 ((Ability)context).updateForm(formId,bindingData);

  1. @Override 
  2.    public void updateFormData(long formId, Object... vars) { 
  3.        ZSONObject zsonObject=new ZSONObject(); 
  4.        zsonObject.put("searchBtns",defualtBtn); 
  5.        zsonObject.put("searchText",markType); 
  6.        String mapName="amap"+System.currentTimeMillis()+".png"
  7.        zsonObject.put("imgSrc","memory://"+mapName); 
  8.        FormBindingData bindingData = new FormBindingData(zsonObject); 
  9.        String amapUrl=getMapImageUrl(mKeyLocation); 
  10.        byte[] bytes= HttpImageUtils.doGetRequestForFile(amapUrl); 
  11.        bindingData.addImageData(mapName,bytes); 
  12.        try{ 
  13.            ((Ability)context).updateForm(formId,bindingData); 
  14.        }catch (Exception ex){ 
  15.            ex.printStackTrace(); 
  16.        } 
  17.  
  18.    } 

其它一些上述方法中調(diào)用的私有方法及類

私有方法:

  1. private void refreshMark(){ 
  2.       try{ 
  3.           this.mKeyLocation= HttpImageUtils.SearchByKeyUrl(getMapMarkUrl(10)); 
  4.       }catch (Exception ex){ 
  5.           ex.printStackTrace(); 
  6.       } 
  7.   } 
  8.  
  9.   private String getMapImageUrl(List<String> Position){ 
  10.       String url="https://restapi.amap.com/v3/staticmap"
  11.       String params="key="
  12.       params+="&zoom="+mRoom; 
  13.       params+="&size="+mSize; 
  14.       if(slocation!=null
  15.           params+="&location="+slocation.getLongitude()+","+slocation.getLatitude(); 
  16.       params+="&markers=large,0xea7700,H:"+slocation.getLongitude()+","+slocation.getLatitude(); 
  17.       if(Position==null || Position.size()==0) 
  18.           return  url+"?"+params; 
  19.       String markers="|mid,0xFF0000,:"
  20.  
  21.       for(int i=0;i<Position.size();i++){ 
  22.           markers+=Position.get(i)+";"
  23.       } 
  24.       params+=markers.substring(0,markers.length()-1); 
  25.       return url+"?"+params; 
  26.   } 
  27.  
  28.   private  String getMapMarkUrl(int size){ 
  29.       String Url="https://restapi.amap.com/v5/place/around?key="
  30.       Url+="&keywords="+(markType=="未設(shè)置"?"":markType); 
  31.       if(slocation!=null
  32.           Url+="&location="+slocation.getLongitude()+","+slocation.getLatitude(); 
  33.       Url+="&size="+size
  34.       return Url; 
  35.   } 

HttpImageUtils類

  1. package com.panda_coder.amapcard.utils; 
  2.  
  3. import com.panda_coder.amapcard.MainAbility; 
  4. import ohos.hiviewdfx.HiLog; 
  5. import ohos.hiviewdfx.HiLogLabel; 
  6. import ohos.utils.zson.ZSONArray; 
  7. import ohos.utils.zson.ZSONObject; 
  8.  
  9. import java.io.*; 
  10. import java.net.HttpURLConnection; 
  11. import java.net.URL; 
  12. import java.util.ArrayList; 
  13. import java.util.List; 
  14.  
  15. public class HttpImageUtils { 
  16.     private static final HiLogLabel TAG = new HiLogLabel(HiLog.DEBUG, 0x0, MainAbility.class.getName()); 
  17.  
  18.     public final static byte[] doGetRequestForFile(String urlStr) { 
  19.         InputStream is = null
  20.         HttpURLConnection conn = null
  21.         byte[] buff = new byte[1024]; 
  22.         try { 
  23.             URL url = new URL(urlStr); 
  24.             conn = (HttpURLConnection) url.openConnection(); 
  25.  
  26.             conn.setDoInput(true); 
  27.             conn.setRequestMethod("GET"); 
  28.             conn.setReadTimeout(6000); 
  29.             conn.connect(); 
  30.             is = conn.getInputStream(); 
  31.             if (conn.getResponseCode() == 200) { 
  32.                 buff = readInputStream(is); 
  33.             } else
  34.                 buff=null
  35.             } 
  36.         } catch (Exception e) { 
  37.             HiLog.error(TAG,"【獲取圖片異?!?quot;,e); 
  38.         } 
  39.         finally { 
  40.             try { 
  41.                 if(is != null){ 
  42.                     is.close(); 
  43.                 } 
  44.             } catch (IOException e) { 
  45.                 e.printStackTrace(); 
  46.             } 
  47.             conn.disconnect(); 
  48.         } 
  49.  
  50.         return buff; 
  51.     } 
  52.  
  53.     public static byte[] readInputStream(InputStream is) { 
  54.         ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
  55.         byte[] buffer = new byte[1024]; 
  56.         int length = -1; 
  57.         try { 
  58.             while ((length = is.read(buffer)) != -1) { 
  59.                 baos.write(buffer, 0, length); 
  60.             } 
  61.             baos.flush(); 
  62.         } catch (IOException e) { 
  63.             e.printStackTrace(); 
  64.         } 
  65.         byte[] data = baos.toByteArray(); 
  66.         try { 
  67.             is.close(); 
  68.             baos.close(); 
  69.         } catch (IOException e) { 
  70.             e.printStackTrace(); 
  71.         } 
  72.         return data; 
  73.     } 
  74.  
  75.     public static String httpGet(String urlStr){ 
  76.         InputStream is = null
  77.         HttpURLConnection conn = null
  78.         String response=""
  79.         StringBuffer buffer = new StringBuffer(); 
  80.         try { 
  81.             URL url = new URL(urlStr); 
  82.             conn = (HttpURLConnection) url.openConnection(); 
  83.  
  84.             conn.setDoInput(true); 
  85.             conn.setRequestMethod("GET"); 
  86.             conn.setReadTimeout(6000); 
  87.             conn.connect(); 
  88.             is = conn.getInputStream(); 
  89.             if (conn.getResponseCode() == 200) { 
  90.                 String str=null
  91.                 InputStreamReader isr = new InputStreamReader(is,"utf-8"); 
  92.                 BufferedReader br = new BufferedReader(isr); 
  93.                 while((response = br.readLine())!=null){ 
  94.                     buffer.append(response); 
  95.                 } 
  96.             } 
  97.             response=buffer.toString(); 
  98.  
  99.         } catch (Exception e) { 
  100.             HiLog.error(TAG,"【訪問(wèn)異?!?quot;,e); 
  101.         } 
  102.         finally { 
  103.             try { 
  104.                 if(is != null){ 
  105.                     is.close(); 
  106.                 } 
  107.             } catch (IOException e) { 
  108.                 e.printStackTrace(); 
  109.             } 
  110.             conn.disconnect(); 
  111.         } 
  112.         return response; 
  113.     } 
  114.  
  115.     public final  static List<String> SearchByKeyUrl(String urlStr){ 
  116.         List<String> result=new ArrayList<>(); 
  117.         String response= httpGet(urlStr); 
  118.         if(response==null || response==""
  119.             return result; 
  120.         ZSONObject zson=ZSONObject.stringToZSON(response); 
  121.         if(zson.getIntValue("infocode")!=10000) 
  122.             return result; 
  123.         ZSONArray zsonArray=zson.getZSONArray("pois"); 
  124.         for(int i=0;i<zsonArray.size();i++){ 
  125.             ZSONObject child= (ZSONObject)zsonArray.get(i); 
  126.             String location=child.getString("location"); 
  127.             result.add(location); 
  128.         } 
  129.         return result; 
  130.     } 

想了解更多內(nèi)容,請(qǐng)?jiān)L問(wèn):

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.51cto.com

 

責(zé)任編輯:jianghua 來(lái)源: 鴻蒙社區(qū)
相關(guān)推薦

2023-04-07 09:20:55

2021-08-18 10:06:33

鴻蒙HarmonyOS應(yīng)用

2023-03-22 09:00:38

2021-08-23 10:12:41

鴻蒙HarmonyOS應(yīng)用

2021-09-15 10:19:15

鴻蒙HarmonyOS應(yīng)用

2021-09-18 09:57:20

鴻蒙HarmonyOS應(yīng)用

2021-06-21 15:21:52

鴻蒙HarmonyOS應(yīng)用開(kāi)發(fā)

2023-06-20 15:45:15

服務(wù)卡片鴻蒙

2021-07-22 08:45:47

鴻蒙HarmonyOS應(yīng)用

2023-08-04 17:24:43

2022-04-24 15:26:38

服務(wù)卡鴻蒙

2023-09-13 15:27:14

鴻蒙應(yīng)用元服務(wù)

2021-06-23 16:05:05

鴻蒙HarmonyOS應(yīng)用

2021-07-07 14:03:12

鴻蒙HarmonyOS應(yīng)用

2023-09-18 15:12:47

服務(wù)卡片ArkTS

2022-05-09 11:52:38

Java卡片服務(wù)卡片

2021-08-20 14:23:14

鴻蒙HarmonyOS應(yīng)用

2021-09-09 15:07:36

鴻蒙HarmonyOS應(yīng)用

2021-07-20 09:58:16

鴻蒙HarmonyOS應(yīng)用
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)