HTML5 geographical location, Baidu Map API, knowledge points familiar

Source: Internet
Author: User

infer compatibility issues with browsers:ie9+ supports geolocation and the FF Chrome version supports geo-targeting
if (navigator.geolocation) {
Alert (' Support geo-location ');
} else {
Alert (' geo-location not supported ');
}

get the latitude and longitude of your position
<script>
var X=document.getelementbyid ("Wrap");
function GetLocation () {
if (navigator.geolocation) {
Navigator.geolocation.GetCurrentPosition(showposition);
} else {
Alert (' Don't support geo-targeting--HTML5 free! ‘);
}
} getLocation ();

function Showposition (position) {
x.innerhtml = "Latitude:" +Position.coords.latitude+ "<br/> Longitude:" +Position.coords.longitude;

}
</script>

Handling Errors and rejections
getcurrentposition (number 1, number of 2); The second parameter of the method is used to handle an error. It specifies the function that executes when the user location fails to get

Error code:

    • Permission denied-user does not agree geo-location
    • Position unavailable-Unable to get current position
    • Timeout-operation timed out

/ * Display positioning error * /
function ShowError (Error) {
SwitchError.code) {
Caseerror. Position_unavailable:
X.innerhtml= "Cannot get the current location!

"; Break
Case error. Permission_denied:
X.innerhtml= "Users disagree with Geo-targeting!

"; Break
Case error. TIMEOUT:
X.innerhtml= "Operation timed out! "; Break
Case error. Unknown_error:
X.innerhtml= "Unknown Error! " ; Break
}
}


watchPosition 用法跟getCurrenPosition方法类似 。watchposition returns the user's current location, assuming that the user moves back to the real-time location, just like a GPS. 

①navigator.geolocation. watchposition (showposition);
Clearwatch ()-Stop Watchposition () method

? Baidu Map API Assistant http://developer.baidu.com/map/
/* latitude and longitude displayed on the map */

function Showonmap (position) { var latlon=position.coords.latitude+ "," +position.coords.longitude;< /c4> var img_url= "Http://maps.googleapis.com/maps/api/staticmap?

center= " +latlon+" &zoom=14&size=400x300&sensor=false "; document.getElementById ("Mapholder"). Innerhtml= " ';       }


Demo Demo: Display latitude and longitude call Baidu Map API display map Information

HTML5 geographical location, Baidu Map API, knowledge points familiar

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.