Home >Web Front-end >H5 Tutorial >Example of html5 positioning and display on Baidu map_html5 tutorial skills
When developing mobile web or webapp, when using Baidu Map API, it is often necessary to obtain the current location through mobile phone positioning and display it in the center on the map. This requires the use of the geolocation function of HTML5.
After successfully obtaining the coordinates, the callback function callback will be executed; the parameter of the callback method is the obtained coordinate point; then you can initialize the map, set the control, center point, zoom level, and then add a point overlay to the map:
However, in fact, this is not enough, and the displayed results are not accurate. This is because the coordinates obtained by getCurrentPosition are GPS longitude and latitude coordinates, and the coordinates of Baidu Map have been specially converted. Therefore, after obtaining the positioning coordinates and initializing One-step coordinate conversion is required between maps. This conversion method is already provided in Baidu API. Methods for converting a single point or batch conversion are provided: single point conversion needs to refer to http://developer.baidu.com/map/ jsdemo/demo/convertor.js, batch conversion needs to refer to http://developer.baidu.com/map/jsdemo/demo/changeMore.js, only the former is needed here:
The detailed code of the example is as follows: (the ak in the reference is the application key)
During the development process, I felt that the computer's positioning speed was a bit slow. It was often impossible to obtain coordinates and the map could not be displayed. It is recommended to use a mobile phone to test, as the positioning is faster.
Of course, if you are only developing mobile web pages, you do not need to use jQuery. The frame is too large, so you can use other lightweight mobile js frameworks.