google geolocation api没有返回输出? [英] google geolocation api not returning the output?

查看:78
本文介绍了google geolocation api没有返回输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我试图通过使用谷歌的地理定位API来获取用户的位置,但我在PC(winnodws7,firefox 8)和时间中获得空值移动中的错误(三星galaxy s3 mini,android,firefox)。我已经使用了api键并激活了api但是我无法获得该位置,我会将我的代码粘贴到下面



Hi,

I tried to get the location of user by using google's geolocation api but I am getting null value in PC(winnodws7,firefox 8) and time out error in mobile(samsung galaxy s3 mini,android,firefox).I have taken api key and activated the api but stil I am not able to get the location,I ll paste my code below

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>Find a route using Geolocation and Google Maps API</title>
    <script src="http://maps.google.com/maps/api/js?key=myKey&sensor=true"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script>
      function calculateRoute(from, to) {
        // Center initialized to Naples, Italy
        var myOptions = {
          zoom: 10,
          center: new google.maps.LatLng(40.84, 14.25),
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        // Draw the map
        var mapObject = new google.maps.Map(document.getElementById("map"), myOptions);
        var directionsService = new google.maps.DirectionsService();
        var directionsRequest = {
          origin: from,
          destination: to,
          travelMode: google.maps.DirectionsTravelMode.DRIVING,
          unitSystem: google.maps.UnitSystem.METRIC
        };
        directionsService.route(
          directionsRequest,
          function(response, status)
          {
            if (status == google.maps.DirectionsStatus.OK)
            {
              new google.maps.DirectionsRenderer({
                map: mapObject,
                directions: response
              });
            }
            else
              $("#error").append("Unable to retrieve your route<br />");
          }
        );
      }
      $(document).ready(function() {
        // If the browser supports the Geolocation API
        if (typeof navigator.geolocation == "undefined") {
          $("#error").text("Your browser doesn't support the Geolocation API");
          return;
        }
        $("#from-link, #to-link").click(function(event) {
          event.preventDefault();
          var addressId = this.id.substring(0, this.id.indexOf("-"));
          navigator.geolocation.getCurrentPosition(function(position) {
            var geocoder = new google.maps.Geocoder();
            geocoder.geocode({
              "location": new google.maps.LatLng(position.coords.latitude, position.coords.longitude)
            },
            function(results, status) {
              if (status == google.maps.GeocoderStatus.OK)
                $("#" + addressId).val(results[0].formatted_address);
              else
                $("#error").append("Unable to retrieve your address<br />");
            });
          },
          function(positionError){
            $("#error").append("Error: " + positionError.message + "<br />");
          },
          {
            enableHighAccuracy: true,
            timeout: 10 * 1000 // 10 seconds
          });
        });
        $("#calculate-route").submit(function(event) {
          event.preventDefault();
          calculateRoute($("#from").val(), $("#to").val());
        });
      });
    </script>
    <style type="text/css">
      #map {
        width: 500px;
        height: 400px;
        margin-top: 10px;
      }
    </style>
  </head>
  <body>
    <h1>Calculate your route</h1>
    <form id="calculate-route" name="calculate-route" action="#" method="get">
      <label for="from">From:</label>
      <input type="text" id="from" name="from" required="required" placeholder="An address" size="30" />
      <a id="from-link" href="#">Get my position</a>
      <br />
      <label for="to">To:</label>
      <input type="text" id="to" name="to" required="required" placeholder="Another address" size="30" />
      <a id="to-link" href="#">Get my position</a>
      <br />
      <input type="submit" />
      <input type="reset" />
    </form>
    <div id="map"></div>
    <p id="error"></p>
  </body>
</html>





请让我知道

1)为什么我没有得到理想的结果

2)要求是什么(ha rdware和软件)在PC上测试程序

3)在移动设备中测试程序的要求(硬件和软件)



先谢谢



Please let me know
1) Why I am not getting the desired result
2) What is the requirements(hardware and software) to test the program in PC
3)What is the requirements (hardware and software) to test the program in mobile device

Thanks in Advance

推荐答案

#error )。append( 无法检索您的路线< br />) ;
}
);
}
("#error").append("Unable to retrieve your route<br />"); } ); }


document )。ready( function (){
// 如果浏览器支持Geolocation API
< span class =code-keyword> if ( typeof navigator .geolocation == < span class =code-string> undefined){
(document).ready(function() { // If the browser supports the Geolocation API if (typeof navigator.geolocation == "undefined") {


#error)。text( 您的浏览器不支持Geolocation API);
return ;
}
("#error").text("Your browser doesn't support the Geolocation API"); return; }


这篇关于google geolocation api没有返回输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆