【问题标题】:How to use gps in android with locationManager?如何在带有locationManager的android中使用gps?
【发布时间】:2017-04-20 12:56:29
【问题描述】:

我看了很多关于它的讨论,但我仍然没有成功在带有位置管理器的 android 应用中显示 gps 坐标:

如果我理解得很好,我必须先做:

LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

向位置经理收费,之后

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, (LocationListener) this);

更新位置 最后是

Location position = locationManager.getLastKnownLocation( LocationManager.GPS_PROVIDER);

问题是这不起作用。所以我想我没有很好地理解。请告诉我什么是好的语法。

提前感谢您的帮助!

编辑:

我在清单中添加了这些权限

 <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <uses-feature android:name="android.hardware.location.network" />
    <uses-feature android:name="android.hardware.location.gps" />
    <uses-feature android:name="android.hardware.wifi" />

【问题讨论】:

  • 您是否在清单中添加了权限或 API 的运行时权限>=23?
  • 我使用添加的权限编辑了我的消息。这个“API>=23 的运行时权限”是什么意思?
  • 如果您使用的是安卓设备棉花糖和牛轧糖。然后你必须提供运行时权限。
  • 好的,谢谢,我如何添加这些权限?有没有类似 的东西?
  • 不,这不是运行时权限。像这样androidhive.info/2016/11/…

标签: java android gps android-gps


【解决方案1】:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtLat = (TextView) findViewById(R.id.textview1);

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}
@Override
public void onLocationChanged(Location location) {
txtLat = (TextView) findViewById(R.id.textview1);
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");
}

【讨论】:

  • 非常感谢您的回答,但我无法编译您的代码...您能提供一个可以编译的版本吗?提前非常感谢您!
  • Error:(32, 24) 错误:找不到合适的方法 requestLocationUpdates(String,int,int,Gpslocation) 方法 LocationManager.requestLocationUpdates(String,long,float,LocationListener) 不适用(参数不匹配;Gpslocation 无法转换为 LocationListener) 方法 LocationManager.requestLocationUpdates(String,long,float,PendingIntent) 不适用(参数不匹配;Gpslocation 无法转换为 PendingIntent)方法 LocationManager.requestLocationUpdates(long,float,Criteria,PendingIntent) 是不适用(参数不匹配;字符串不能转长)
  • 没有 cmets,没有解释,格式错误。仅代码答案 = 错误答案。
【解决方案2】:

获取位置的代码 sn-p。

LocationManager locationManager;

locationManager = (LocationManager) getSystemService(context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
    return;// and ask for run time permission
}
Location currentLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (currentLocation != null) {
    mypoint = new GeoPoint(currentLocation.getLatitude(), currentLocation.getLongitude());
}
markerForCurrentLocation.setPosition(mypoint);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 1, this);

@Override
public void onLocationChanged(Location location) {

    //Here you will get the current location of your GPS device
    mypoint = new GeoPoint(location.getLatitude(), location.getLongitude());
    markerForCurrentLocation.setPosition(mypoint);
    mapController.animateTo(mypoint);
}

请求运行时位置权限的方法

private void requestLocationPermission() {

    //method asking for runtime location permission
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_PERMISSION_CODE);
    }
}

【讨论】:

    猜你喜欢
    • 2013-06-14
    • 1970-01-01
    • 1970-01-01
    • 2019-10-09
    • 1970-01-01
    • 1970-01-01
    • 2011-10-23
    • 2018-12-19
    • 1970-01-01
    相关资源
    最近更新 更多