Using HTML5 to develop Android (5)---HTML5 geolocation service for Android applications

Source: Internet
Author: User

In Android
Java code
1 //enable geo-targeting2Websettings.setgeolocationenabled (true); 3 //set the location of the database path4 Websettings.setgeolocationdatabasepath (dir); 5   6 //Configure permissions (also implemented in webchromeclient)7  Public voidongeolocationpermissionsshowprompt (String origin,8 geolocationpermissions.callback Callback) {   9Callback.invoke (Origin,true,false); Ten     Super. Ongeolocationpermissionsshowprompt (origin, callback);  One}
Adding Permissions in Manifest
XML code
1 <  android:name= "Android.permission.ACCESS_FINE_LOCATION"/>  2  <android:name= "Android.permission.ACCESS_COARSE_LOCATION" />  
   in HTML5;  obtaining geo-location information from navigator.geolocation objects  
Common Navigator.geolocation objects are available in the following three ways:

 1  //  2     Navigator.geolocation.getCurrentPosition (Success_callback_function, error_callback_function, position_options)  3  //  4   Navigator.geolocation.watchPosition (Success_callback_function, Error_callback_function, position_options)  5    Clear the persistent get geo-location event  6  Navigator.geolocation.clearWatch (watch_position_id) 
Where Success_callback_function is the function that is processed after success, Error_callback_function is the handler function returned after the failure, the parameter position_options is the configuration item

The code in JS
JS Code
1 //positioning2 functionget_location () {3     if(navigator.geolocation) {4Navigator.geolocation.getCurrentPosition (show_map,handle_error,{enablehighaccuracy:false, maximumage:1000,timeout:15000}); 5}Else {   6Alert ("Your browser does not support HTML5 geolocation"); 7     }   8 }   9        Ten functionShow_map (position) { One     varLatitude =Position.coords.latitude;  A     varLongitude =Position.coords.longitude;  -     varCity =position.coords.city;  -     //telnet localhost 5554 the     //Geo fix-82.411629 28.054553 -     //Geo fix-121.45356 46.51119 4392 -     //Geo Nmea $GPGGA, 001431.092,0118.2653,n,10351.1359,e,0,00,,-19.6,m,4.1,m,,0000*5b -Document.getelementbyidx_x_x_x ("Latitude"). Innerhtml= "Latitude:" +latitude;  +Document.getelementbyidx_x_x_x ("Longitude"). Innerhtml= "Longitude:" +longitude;  -Document.getelementbyidx_x_x_x ("City"). Innerhtml= "City:" +City ;  + }    A         at functionHandle_error (err) { -     Switch(err.code) { -      Case1:    -Alert ("Permission Denied");  -          Break;  -      Case2:    inAlert ("The network is down or the position satellites can ' t be contacted");  -          Break;  to      Case3:    +Alert ("Time Out");  -          Break;  the     default:    *Alert ("Unknown error");  $          Break; Panax Notoginseng     }    -}
Where the Position object contains many data error codes and options to view the document

Using HTML5 to develop Android (5)---HTML5 geolocation service for Android applications

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.