通过处理在android上通过GPS获取位置

时间:2018-11-07 20:33:22

标签: java permissions location processing android-gps

我正在开发一个需要访问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) {
  }
}

1 个答案:

答案 0 :(得分:0)

解决方案:

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

  • 也不要使用NETWORK_PROVIDER来使用GPS_PROVIDER来获取真正的GPS pos,而不要使用网络pos。