【问题标题】:getting location via GPS on android with Processing通过 android 上的 GPS 获取位置与处理
【发布时间】:2018-11-07 20:33:22
【问题描述】:

我正在开发一个需要访问 GPS 的小型应用程序,以便我可以跟踪我的位置。 但是我粘贴了一些代码,我可以用来检查它是否有效。我稍后会重写它,以便可以对其进行调整,但现在我只想尝试一下。 但是当我执行应用程序时,所有参数都保持初始化的方式。 我确实启用了所有权限并启用了 GPS。甚至去外面检查它是否有效,但它总是保持不变。 在应用程序询问我是否允许应用程序使用 gps 服务后,一切都正确执行。它对位置跟踪返回正值。

这里是代码:(也可以在这里找到:https://github.com/codeanticode/processing-android-tutorials/blob/master/location_permissions/ex1_gps/ex1_gps.pde

/*****************************************************************************************
 Android Processing GPS example

 Query the phone's GPS and display the data on the screen

 Rolf van Gelder - v 22/02/2011 - http://cage.nl :: http://cagewebdev.com :: info@cage.nl

 Check the ACCESS_FINE_LOCATION permission in Sketch Permissions!

 *****************************************************************************************/

// Import needed Android libs
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.provider.Settings;
import android.os.Bundle;
import android.Manifest;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;

// Set up the variables for the LocationManager and LocationListener
LocationManager locationManager;
MyLocationListener locationListener;

// Variables to hold the current GPS data
float currentLatitude  = 0;
float currentLongitude = 0;
float currentAccuracy  = 0;
String currentProvider = "";

boolean hasLocation = false;

void setup () {
  fullScreen();
  orientation(PORTRAIT);  
  textFont(createFont("SansSerif", 26 * displayDensity));
  textAlign(CENTER, CENTER);
  requestPermission("android.permission.ACCESS_FINE_LOCATION", "initLocation");
}

void draw() {
  background(0);
  if (hasPermission("android.permission.ACCESS_FINE_LOCATION")) {
    text("Latitude: " + currentLatitude + "\n" +
         "Longitude: " + currentLongitude + "\n" +
         "Accuracy: " + currentAccuracy + "\n" +
         "Provider: " + currentProvider, 0, 0, width, height);
  } else {
    text("No permissions to access location", 0, 0, width, height);
  }
}

void initLocation(boolean granted) {
  if (granted) {    
    Context context = getContext();
    locationListener = new MyLocationListener();
    locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);    
    // Register the listener with the Location Manager to receive location updates
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
    hasLocation = true;
  } else {
    hasLocation = false;
  }
}

// Class for capturing the GPS data
class MyLocationListener implements LocationListener {
  public void onLocationChanged(Location location) {
    currentLatitude  = (float)location.getLatitude();
    currentLongitude = (float)location.getLongitude();
    currentAccuracy  = (float)location.getAccuracy();
    currentProvider  = location.getProvider();
  }

  public void onProviderDisabled (String provider) { 
    currentProvider = "";
  }

  public void onProviderEnabled (String provider) { 
    currentProvider = provider;
  }

  public void onStatusChanged (String provider, int status, Bundle extras) {
  }
}

【问题讨论】:

  • 您是否尝试在日志中查找错误?
  • 没有错误。一切都运行顺利,除了它没有得到数据。它识别出gps权限存在并允许gps访问

标签: java permissions location processing android-gps


【解决方案1】:

解决办法:

  • locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); 中的“0, 0”更改为“1000, 0”,具体取决于在这种情况下您希望获得当前位置的频率:1000 毫秒。第二个0我就不说了。

  • 也不要使用NETWORK_PROVIDER,而是使用GPS_PROVIDER 来获取真正的GPS 位置,而不是联网位置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-17
    • 1970-01-01
    • 1970-01-01
    • 2013-12-11
    相关资源
    最近更新 更多