地理位置示例代码查找HTML 5附近的特定商店

时间:2014-08-15 10:09:33

标签: html5 geolocation maps

是否有任何想法使用Geolocation API查找我所在位置附近的商店?一个示例代码应该没问题然后我会继续。

1 个答案:

答案 0 :(得分:1)

您可以使用地理位置API获取当前位置。

然后你给谷歌地图的经度和纬度以及搜索查询,谷歌地图将返回你的地图,你必须把标记位置放在哪里:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>Reports</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="google" value="notranslate" />
        <meta name="application-name" content="GeoLocator" />
        <meta name="description" content="HTML5&#39;s approach to email" />
        <meta name="application-url" content="https://developer.mozilla.org" />
        <meta name="google" content="notranslate" />
        <!--
        <link rel="canonical" href="https://mail.google.com/mail/" />
        <link rel="shortcut icon" href="https://ssl.gstatic.com/ui/v1/icons/mail/images/favicon2.ico" type="image/x-icon" />
        <link rel="alternate" type="application/atom+xml" title="Gmail Atom Feed" href="feed/atom" />
            -->
        <script type="text/javascript">
            // <![CDATA[
            var options = {
                enableHighAccuracy: true,
                timeout: 5000,
                maximumAge: 0
            };


            var x = null;

            function success(position) {
                var lat = position.coords.latitude;
                var lon = position.coords.longitude;
                var accuracyInMeters = position.coords.accuracy;


                x.innerHTML = "Latitude: " + position.coords.latitude + "<br>Longitude: " + position.coords.longitude;
            }


            function error(err)
            {
                console.warn('ERROR(' + err.code + '): ' + err.message);
                x.innerHTML = '<span style="color: red;">No fix on location</span>';
            }

            function getLocation() {
                if (navigator.geolocation) {
                    // https://developer.mozilla.org/en-US/docs/Web/API/Geolocation.getCurrentPosition
                    // First of all, realise that the default timeout for getCurrentPosition is infinite(!). 
                    // That means that your error handler will never be called if getCurrentPosition hangs somewhere on the back end.

                    navigator.geolocation.getCurrentPosition(success, error, options); // Error and optios can be omitted
                } else {
                    alert("not supported")
                    x.innerHTML = "Geolocation is not supported by this browser.";
                }

            }





            document.addEventListener('DOMContentLoaded', function ()
            {
                x = document.getElementById("demo");
                /*fun code to run*/
                getLocation();
            })




            // ]]>
        </script>


        <style type="text/css" media="all">
            html, body {
                width: 100%;
                height: 100%;
                margin: 0px;
                padding: 0px;
            }

        </style>


    </head>
    <body>
        <div id="demo"></div>

    </body>
</html>

如果浏览器不支持地理位置,那么您可以使用   - 使用IP解析城镇的地理坐标,并使用其长/拉   - 或者您可以要求用户输入他的地址,并从那里获得经度/纬度:

function searchLocations() {
  var address = document.getElementById("addressInput").value;
  var geocoder = new google.maps.Geocoder();
  geocoder.geocode({address: address}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      searchLocationsNear(results[0].geometry.location);
    } else {
      alert(address + ' not found');
    }
  });
}

您可以将商店的位置放入数据库表并在那里查找其坐标,而不是从谷歌中获取最近的商店。
看这里的例子: https://developers.google.com/maps/articles/phpsqlsearch_v3

要获取某个位置附近的商店列表,您可以使用 Google Places API
https://developers.google.com/places/documentation/?csw=1
或者你可以在这里使用StoreLocater库:
http://storelocator.googlecode.com/git/index.html