Home  >  Article  >  Web Front-end  >  How to use phonegap to obtain location information

How to use phonegap to obtain location information

黄舟
黄舟Original
2017-05-21 14:41:031681browse

The following editor will bring you an implementation method of using phonegap to obtain location information. The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor and take a look.

The examples are as follows:

<!DOCTYPE html> 
<html> 
  
    <head> 
        <meta charset="UTF-8"> 
        <title>Compass Example</title> 
  
        <script type="text/javascript" charset="UTF-8" src="cordova.js"></script> 
        <script type="text/javascript" charset="UTF-8"> 
            document.addEventListener("deviceready", onDeviceReady, false); 
  
            function onDeviceReady() { 
                getPosition(); 
            } 
              
            //获取当前位置信息 
            function getPosition() { 
                navigator.geolocation.getCurrentPosition(onSuccess,onError); 
            } 
  
  
            //错误的回调  
            function onError(error) { 
                alert(&#39;code:&#39;+error.code+&#39;\n&#39;+&#39;message:&#39;+error.message+&#39;\n&#39;); 
            } 
  
            //成功的回调  
            function onSuccess(position) { 
                alert(&#39;Latitude:&#39;+position.coords.latitude+&#39;\n&#39;+ 
                &#39;Longitude:&#39;+position.coords.longitude+&#39;\n&#39;+ 
                &#39;Altitude:&#39;+position.coords.altitude+&#39;\n&#39;+ 
                &#39;Accuracy:&#39;+position.coords.accuracy+&#39;\n&#39;+ 
                &#39;Altitude Accuracy:&#39;+position.coords.altitudeAccuracy+&#39;\n&#39;+ 
                &#39;Heading:&#39;+position.coords.heading+&#39;\n&#39;+ 
                &#39;Speed:&#39;+position.coords.speed+&#39;\n&#39;+ 
                &#39;Timestamp:&#39;+new Date(position.timeStamp)+&#39;\n&#39;); 
            } 
        </script> 
    </head> 
  
    <body> 
        <button onclick="getPosition();">Start Watching</button> 
    </body> 
  
</html>

The above is the detailed content of How to use phonegap to obtain location information. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn