微信开发系列之八 - 微信公众号的地图集成

文章系列目录

In Wechat development series 5 – embedded your UI5 application to Wechat app we have learned the steps how to put a web page into the Wechat app. Use the same approach, we can enable our subscription account with a little bit more feature: the map integration.
When a user has subscribed the test account, the Map integration menu is available:

微信开发系列之八 - 微信公众号的地图集成_第1张图片

The user can type the address manually and press button “Search” to locate the address in the map view.

微信开发系列之八 - 微信公众号的地图集成_第2张图片

You can use the control highlighted below to zoom in / out the map or switch the map type.

微信开发系列之八 - 微信公众号的地图集成_第3张图片

There are many free map providers available and in this blog I choose a popular provider in China: Baidu map. The same idea could be applied to other map provider as well.
In fact all the map related functionality such as location search, result render and the controls mentioned above are all implemented by map provider, all our needed to develop is just a wrapper html page where we get the search address input by end user, and call the search API, that’s all.
The functionality I show in this blog only needs 100 lines of html code to implement, unbelievable?

(1) Create a folder named “map”, and two files “baidu.js” and “bmap.css” inside it, whose source code could be found from my github.

(2) create an index.html and paste the following source code:




    
    
    Jerry's map practice







        

I didn’t use any frontend framework to make this example simple and easily understood. When the map view is initially rendered, onload function findDefaultLocation() will be fired to locate the hard coded address “成都” in the map. End user can also change this address maintained in textarea and press search button to locate new address, implemented by function search().

微信开发系列之八 - 微信公众号的地图集成_第4张图片

(3) Add one line below in server.js, to support route based on url /map to the created folder in step one.

微信开发系列之八 - 微信公众号的地图集成_第5张图片

Redeploy the whole application to cloud platform.

After that the index.html could be accessed via url in my laptop:

微信开发系列之八 - 微信公众号的地图集成_第6张图片

(4) Create a new menu for your Wechat subscription account by calling Restful API with the following payload:

{
                 "button":[
                     {
                       "name":"Web Application",
                       "sub_button":[{
                            "type": "view",
                            "name": "Map integration",
                            "url": "http://wechatjerry.herokuapp.com/map"
                       },{
                            "type": "click",
                            "name": "Other UI5 application",
                             "key": "dataQuery"
                       }]
                     }
                 ]
  }

Now the whole scenario could be tested from your Wechat application.

要获取更多Jerry的原创文章,请关注公众号"汪子熙":

你可能感兴趣的:(微信,微信小程序,微信开发,微信公众平台,sap)