使用 Processing 在 android 上通过 GPS 获取位置

getting location via GPS on android with Processing

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

这是代码:(也可以在这里找到: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) {
  }
}

解决方法:

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

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