Mobile H5 method of acquiring the current city

Source: Internet
Author: User
<span id="Label3"></p><p><p></p></p><p><p>The H5 page on the mobile side provides the ability to locate, so how do you achieve one of the simplest requirements-----get the User's current city?</p></p><p><p>You may find the N-blog introduction, but you will find that after reading the large piece of code is still not clear, in order to facilitate the understanding, I streamlined the code, only the necessary parts.</p></p><p><p></p></p><p><p>1, in the HTML page introduced Baidu Map API (document Address: Http://developer.baidu.com/map/wiki/index.php?title=jspopular/guide/introduction)</p></p><pre><pre><span style="color: #0000ff;"><</span><span style="color: #800000;"></span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">= "http://api.map.baidu.com/api?ak= Your AK code &v=2.0&services=false"</span> <span style="color: #0000ff;">></</span> <span style="color: #800000;">Script</span> <span style="color: #0000ff;">></span></pre></pre><p><p></p></p><p><p>2, JS code using H5 geolocation method to obtain coordinates, and then use the Baidu API GetLocation method translated into the results you want</p></p><pre><span style="color: #008080;"><span style="color: #008080;">1</span></span>Navigator.geolocation.getCurrentPosition (<span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span><span style="color: #000000;"><span style="color: #000000;">(position) {</span></span><span style="color: #008080;"><span style="color: #008080;">2</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">var</span></span>Lat =<span style="color: #000000;"><span style="color: #000000;">position.coords.latitude;</span></span><span style="color: #008080;"><span style="color: #008080;">3</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">var</span></span>Lon =<span style="color: #000000;"><span style="color: #000000;">position.coords.longitude;</span></span><span style="color: #008080;"><span style="color: #008080;">4</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">var</span></span>Point =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>Bmap.point (lon, lat);<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Create a coordinate point</span></span><span style="color: #008080;"><span style="color: #008080;">5</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">get address description based on coordinates</span></span><span style="color: #008080;"><span style="color: #008080;">6</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">var</span></span>Mygeo =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Bmap.geocoder ();</span></span><span style="color: #008080;"><span style="color: #008080;">7</span></span>Mygeo.getlocation (point,<span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span><span style="color: #000000;"><span style="color: #000000;">(result) {</span></span><span style="color: #008080;"><span style="color: #008080;">8</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">var</span></span>City =<span style="color: #000000;"><span style="color: #000000;">result.addressComponents.city;</span></span><span style="color: #008080;"><span style="color: #008080;">9</span></span>$ (' Body '<span style="color: #000000;"><span style="color: #000000;">). HTML (city);</span></span><span style="color: #008080;"><span style="color: #008080;">Ten</span></span> <span style="color: #000000;"><span style="color: #000000;"> });</span></span><span style="color: #008080;"><span style="color: #008080;"></span> one</span>});</pre><p><p></p></p><p><p>3. Open your phone and try it.</p></p><p><p>Mobile H5 method of acquiring the current city</p></p></span>

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.