使用javascript获取gps位置

时间:2014-10-20 08:27:57

标签: javascript html5 gps

我正在使用平板电脑,在html应用程序上工作,想要在不使用任何网络连接的情况下获取gps位置。 请建议我简单的方法。

4 个答案:

答案 0 :(得分:3)

使用JS你可以像这样得到它

 <script>
   var x = document.getElementById("demo");
    function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);
    } else {
        x.innerHTML = "Geolocation is not supported by this browser.";
    }
}
function showPosition(position) {
    x.innerHTML = "Latitude: " + position.coords.latitude +  "<br>Longitude: " + position.coords.longitude;
}
</script>

答案 1 :(得分:0)

您必须创建LocationManager类的实例。并且您可以使用getLastKnownLocation方法来查找位置。您必须将Location.GPS_PROVIDER作为参数传递。

LocationManager locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
Location userLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Log.d("Latitude", userLocation.getLatitude());
Log.d("Longitude", userLocation.getLongitude());

答案 2 :(得分:0)

授予权限:

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

获取经度&amp;纬度:

LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();

保存在txt文件中:

public void generateLatLongOnSDCard(String sFileName, String sBody){
    try
    {
        File rootDirFile = new File(Environment.getExternalStorageDirectory(), "LatLong");
        if (!rootDirFile.exists()) {
            rootDirFile.mkdirs();
        }
        File gpxfile = new File(rootDirFile, sFileName);
        FileWriter writer = new FileWriter(gpxfile);
        writer.append(sBody);
        writer.flush();
        writer.close();
        Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show();
    }
    catch(IOException e)
    {
         e.printStackTrace();
    }
   }

最后你可以阅读文本文件。

答案 3 :(得分:0)

如果您想获得更准确的位置Feed,您应该请求更新,最后的已知位置对您来说是不够的。 GPS_PROVIDER将仅提供来自gps的位置更新,而不是来自网络提供商。但是,如果您使用NETWORK_PROVIDER,则可以在室内获得更准确的位置值。

public class MainActivity extends Activity implements LocationListener{

    protected LocationManager locationManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
    }

    @Override
    public void onLocationChanged(Location location) {
        txtLat.setText("Latitude:" + location.getLatitude() + ", Longitude:" + location.getLongitude());
    }

    @Override
    public void onProviderDisabled(String provider) {
        Log.d("Latitude","disable");
    }

    @Override
    public void onProviderEnabled(String provider) {
        Log.d("Latitude","enable");
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        Log.d("Latitude","status");
    }
}