HTML5 use Baidu Map API to get the current location

Source: Internet
Author: User

because the project needs to locate the city, studied the geographical location, made some notes, and share with you ~the development requirement for a project is to get to the current user's location and then provide some services to the user. at this point, you can use twoPositioning method, a GPS positioning, and then the positioning of the latitude and longitude transferred to the Baidu Map API interface. Another way is to use the Baidu API interface, using IP address positioning, and then use the IP address to obtain the latitude and longitude, passed to the Baidu Map API interface.try these two ways to locate yourself. Relatively speaking, the IP address location, the deviation is relatively large. GPS positioning is still possible, but not accurate enough. The reasons for the large deviations are likely to be caused by the following points:1, the GPS is opened on the positioning. The data is wrong when the GPS is not searching for enough satellites, or if the signal is bad. 2, did not do GPS coordinates to Baidu coordinate conversion-this reason is also the main reason for inaccurate after getting the latitude and longitude of GPs positioning, in order to make the positioning accurate, we need to "rectify". This is also called "coordinate transformation". There are two methods of coordinate transformation, one is to call the external Web site to provide a corrective method, the other is to use Baidu's own API. Method 1: External Web site correction MethodYou can access the http://ditujiupian.com/Web site and then apply for a test key, which gives you the latitude and longitude you can get after the correction. Method 2: Take advantage of Baidu's own APInot much to say, directly on the code:

<! DOCTYPE html>
<meta Http-equiv="Content-type" Content="Text/html; Charset=utf-8 " />
<meta Name="Viewport" Content="Initial-scale=1.0, User-scalable=no" />
<script Type="Text/javascript" Src="Http://api.map.baidu.com/api?v=2.0&ak= application to Baidu's key"></script>
<title>GPS Switch to Baidu</title>
<body>
<div Id="Allmap"></div>
</body>
<script Type="Text/javascript">
Baidu Map API Features
GPS coordinates
VarXx= 116.397428;
VarYy= 39.90923;
VarGpspoint= New BMap.Point(Xx,Yy);

Map initialization
var BM =new bmap. Map bm. (gpspoint, 15);
bm. Addcontrol (new bmap.< Span class= "Typ" >navigationcontrol ());

//coordinates after conversion of the callback function

This shows the longitude and latitude after correction.
Translatecallback= function (Point){
VarMarker= New BMap.Markerpoint bm.marker bm.point }
bmap. Convertor. Translate (gpspoint,0 , translatecallback //true latitude and longitude converted to Baidu coordinates
</SCRIPT>

HTML5 use Baidu Map API to get the current location

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.