Cordova Development, the Android side using the Baidu SDK positioning.

Source: Internet
Author: User



in this article, thank the author http://snoopyxdy.blog.163.com/blog/static/601174402014420872345/



A recent PhoneGap project to do my job, the most painful is the Android 4.1.x to HTML5 positioning to obtain latitude and longitude has a huge hole, generally we use the following code for HTML5 positioning


Navigator.geolocation.getCurrentPosition (function (POS) {Alert (Json.stringify (POS))},function (err) { Alert (json.stringify (ERR))}, {enablehighaccuracy:false, timeout:60*1000, maximumage:10 00*60*10});


The above code is normal on iOS, the Android 4.3 and later machine can also be accessed normally, but on Android 4.1 will appear timeout, if we set the enablehighaccuracy to true, then in the outdoor, air good place, Android 4.1 will be able to locate through the satellite, but the speed is very slow, in fact, we do not want very accurate positioning information, can be located through the base station or WiFi, although the HTML5 provided us with such an interface, but Android 4.1 is not very good support.



Another huge pit also directly affected my judgment on this question, if I restart the Android 4.1 machine, then the code will be able to run for about a few hours, or even half a day, but the next day will be the same problem, endless timeout. It is not always possible for customers to restart the machine every time they use the positioning function, the machine involved in this problem has included my , Samsung Note2,oppo and other current mainstream Android 4.1 system, in several other units such as glasky3, Red rice and other Android 4.3 machine does not appear.






then I run the above code directly in the browser, Discover Android 4.1 also can't run up, naïve I thought is Android 4.1 to HTML5 interface support problem, so use Cordova load geolocation plug-in, want to use Cordova native method to solve this problem, but the problem is still, which The unexpected discovery of my and Note2 can run, but it just happened to restart.






This is my problem on the StackOverflow, the Chinese and foreign friends who encounter such problems are huge.



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



How to fill this hole? I searched all over the network, found abroad can use Google Maps to solve, and only the use of Baidu Positioning SDK to solve, but the latest Android Baidu positioning SDK4.1 same huge pit, directly download the official demo run on the real machine all kinds of can't locate, has been reported errorcode 602, so stroll the next Baidu SDK Forum, found a lot of people said 4.1 version of the SDK problems, rather use back to 4.0 version, so I reported to try the mood with back to the SDK4.0 version, finally in the Android 4.1 of the Machine positioning success, there is no need to restart the situation, and then I based on Andybuit Author's code, small changes, encapsulated into support for the new version of Cordova 3.4.1 Plug-in on GitHub, for the same problem of friends to use, hoping to help them. Thanks again for the Baidu positioning code provided by Andybuit, thank you!



New Version plugin address:



Https://github.com/DoubleSpout/phonegap_baidu_sdk_location



Below to get to the point, through a simple Android Hello World program to use the Baidu Locator plugin



1. Set up the Hello World project:


Cordova Create Hello Com.example.hello HelloWorld


2, the establishment of Android support, the current plugin only support Android, it seems as long as support for Android, iOS HTML5 positioning support is very good, do not need to locate the SDK Baidu


Cordova Platform Add Android


3, install Baidu positioning plug-in:



You can download the remote from 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 plug-in installation is complete, execute the build command to write the plugin's configuration file to the Android platform directory:


Cordova Build Android


5. Open Eclipse and configure the Hello project:



5.1. New Project









5.2. Add from existing code base









5.3. Find the project path for Android









5.4. Edit Item Properties









5.5, add the Baidu SDK jar file









5.6. Select LOCSDK









5.7. Edit Source Attachment









5.8, the same choice Baidu SDK jar









5.9. Save Path









5.10. Select order and export to tick all previous









5.11. Change the text editing code to Utf-8









5.12. Open the Hello\platforms\android\src\com\spout\phonegap\plugins\baidulocation\baidulocation.java file, Modify your Apkkey (you can actually use it without modification), about 64 lines:






Key generation and access is described in the Baidu SDK key acquisition page, address:



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



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



Ondeviceready : function() { app . receivedevent (' Deviceready ');


       //Through the Baidu SDK to obtain latitude and longitude, and alert out of latitude and longitude information        varNoOp= 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, the completion of the above work, the implementation of command packaging production APK, note that the Baidu Positioning SDK in the simulator is not operational, will expose 167 error code


Cordova Build Android


8, will hello\platforms\android\ant-build\helloworld-debug-unaligned.apk The files are copied to the previously problematic Android 4.1 machine, showing, for example, a quick and easy access to latitude and longitude information via WiFi, and no need to reboot:









At this point, the Baidu SDK is finished, hoping to help the use of PhoneGap to develop Android applications, but also met my friends ~






Cordova Development, the Android side using the Baidu SDK positioning.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.