In the development of cordova, the android side uses Baidu sdk to locate

Reprinted from: http://my.oschina.net/liangzhenghui/blog/339965

A recent phonegap project made me devastated. The most annoying thing is that Android 4.1.x has a huge hole in the positioning of html5 to obtain longitude and latitude. Generally, we use the following code to locate html5

navigator.geolocation.getCurrentPosition(function(pos){               alert(JSON.stringify(pos))        },function(err){               alert(JSON.stringify(err))          }, {    enableHighAccuracy: false,    timeout: 60*1000,    maximumAge: 1000*60*10});

The above code works fine on ios, and can be accessed normally on Android 4.3 and later machines, but there will be a timeout on Android 4.1. If we set  enableHighAccuracy to true, then in a place with good air outdoors, Android 4.1 will It can be positioned by satellite, but the speed is extremely slow. In fact, many times we do not want very accurate positioning information, and can be positioned through base stations or wifi. Although html5 provides us with such an interface, it is supported on Android 4.1. Not very good.

Another giant pit also directly affects my judgment on this problem. If I restart the Android 4.1 machine, the above code will be able to run normally for about a few hours, or even half a day, but the same problem will occur the next day, Endless timeout. It is impossible for customers to restart the machine every time they use the positioning function. The machines involved in this problem include my Huawei, Samsung note2, oppo and other mainstream Android 4.1 systems. In other machines such as glasky3, red rice, etc. It doesn't appear on Android 4.3 machines.

 

Then I ran the above code directly in the browser and found that Android 4.1 could not run either. Naively, I thought it was a problem with Android 4.1's interface support for html5, so I used cordova to load the geolocation plugin, and wanted to use cordova's native method to solve this problem. , but the problem is still the same. I accidentally found that my Huawei and note2 can run, but it just happened to be restarted.

 

This is a question I posted on stackoverflow. There are many Chinese and foreign friends who have encountered this kind of problem.

http://stackoverflow.com/questions/23383750/phonegap-geolocation-sometimes-not-working-on-android

How to fill this hole? I searched the whole network and found that it can be solved by using google maps abroad, while in China, only Baidu's positioning SDK can be used to solve it, but the latest Android Baidu positioning SDK 4.1 is also very pitiful, directly download the official demo and run it in real I couldn't locate all kinds of things on the machine, and kept reporting errorcode 602, so I went to the Baidu SDK forum and found that many people said that the 4.1 version of the SDK has many problems, it is better to use the 4.0 version, so I reported the mood to try and use the SDK4. 0 version, and finally successfully located on the Android 4.1 machine, there is no need to restart, and then I made minor changes according to the code of the author of andybuit, packaged it into a plug-in that supports the new version of cordova 3.4.1 and put it on github, for the same Friends who have problems use it, hope it can help them. Thanks again for the Baidu positioning code provided by andybuit, thank you!

The address of the new version of the plugin:

https://github.com/DoubleSpout/phonegap_baidu_sdk_location

Let's step into the topic, use the Baidu positioning plug-in through a simple Android hello world program

1. Create a hello world project:

cordova create hello com.example.hello HelloWorld

2. Establish Android support. At present, this plug-in only supports Android. It seems that as long as it supports Android, ios supports HTML5 positioning very well, without Baidu positioning SDK

cordova platform add android

3. Install the plugin located by baidu:

You can download the remote through git, you need to install the git command

 cordova plugin add https://github.com/DoubleSpout/phonegap_baidu_sdk_location.git

You can also download the plugin to the hello\plugins directory, rename the folder to com.spout.phonegap.plugins.baidulocation, and then execute the command:

cordova plugin install com.spout.phonegap.plugins.baidulocation

4. After the plugin is installed, execute the build command once to write the plugin's configuration file into the Android platform directory:

cordova build android

5. Open eclipse and configure the hello project:

5.1. New construction

phonegap uses Baidu map sdk to locate - snoopyxdy - snoopyxdy's blog

 

5.2, add from existing code base

phonegap uses Baidu map sdk to locate - snoopyxdy - snoopyxdy's blog

 

5.3. Find the Android project path

phonegap uses Baidu map sdk to locate - snoopyxdy - snoopyxdy's blog

 

5.4. Edit project properties

phonegap uses Baidu map sdk to locate - snoopyxdy - snoopyxdy's blog

 

5.5. Add the jar file of Baidu SDK

phonegap uses Baidu map sdk to locate - snoopyxdy - snoopyxdy's blog

 

5.6. Select locSDK

phonegap uses Baidu map sdk to locate - snoopyxdy - snoopyxdy's blog

 

5.7. Edit source attachment

phonegap uses Baidu map sdk to locate - snoopyxdy - snoopyxdy's blog

 

5.8, also select the jar of Baidu SDK

phonegap uses Baidu map sdk to locate - snoopyxdy - snoopyxdy's blog

 

5.9, save path

phonegap uses Baidu map sdk to locate - snoopyxdy - snoopyxdy's blog

 

5.10. Select order and export to tick all the fronts

phonegap uses Baidu map sdk to locate - snoopyxdy - snoopyxdy's blog

 

5.11. Change the text editing encoding to utf-8

phonegap uses Baidu map sdk to locate - snoopyxdy - snoopyxdy's blog

 

5.12. Open the hello\platforms\android\src\com\spout\phonegap\plugins\baidulocation\BaiduLocation.java file and modify your own apkkey (in fact, it can be used without modification), about line 64:

phonegap uses Baidu map sdk to locate - snoopyxdy - snoopyxdy's blog

For the generation and acquisition of keys, please refer to the key acquisition page of Baidu sdk, address:

http://developer.baidu.com/map/geosdk.htm 

6. Find the hello\www\js\index.js file, modify and add the following lines, and obtain the latitude and longitude through Baidu SDK:

   onDeviceReady:function(){
       app.receivedEvent('deviceready');


       //Get the latitude and longitude through Baidu SDK, and alert out the latitude and longitude information var noop = function (){}        window . locationService . getCurrentPosition ( function ( pos ){            alert ( JSON . stringify ( pos ))            window . locationService . stop ( noop , noop ) }, function ( e ){            alert ( JSON . stringify ( e
       



       
))
           window.locationService.stop(noop,noop)});},
       
   

7. After completing the above work, execute the command to package the production APK. Note that the SDK positioned by Baidu cannot run on the simulator, and the 167 error code will be exposed.

cordova build android

8. Copy the hello\platforms\android\ant-build\HelloWorld-debug-unaligned.apk file to the Android 4.1 machine with previous problems, as shown in the figure below, the latitude and longitude information can be obtained quickly and successfully through wifi, and there is no need to restart The machine is up:

phonegap uses Baidu map sdk to locate - snoopyxdy - snoopyxdy's blog

 

So far, the description of Baidu SDK has been completed. I hope it can help friends who use phonegap to develop Android applications and encounter problems like me~

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327086108&siteId=291194637