为什么我无法在以下棒棒糖版本中搜索位置?

时间:2019-02-09 06:55:14

标签: android

我在以棒棒糖版本加载地图时遇到问题,我在地图中搜索时找不到位置。它显示未找到任何位置,但是可以在marsmallow以上版本中使用。我不知道自己做了什么错误。请任何人提供解决方案以找出答案。

public typealias AnyObject = Builtin.AnyObject

3 个答案:

答案 0 :(得分:1)

在AndroidManifest文件中添加以下权限

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

答案 1 :(得分:0)

首先向清单文件添加权限: 将权限添加到清单后:

int permissionPhoneState = ContextCompat.checkSelfPermission("Your Activity Name", 
Manifest.permission.ACCESS_FINE_LOCATION);
                int permissionPhoneState1 = ContextCompat.checkSelfPermission("Your Activity Name", Manifest.permission.ACCESS_COARSE_LOCATION);
                if ((permissionPhoneState == PackageManager.PERMISSION_GRANTED) && (permissionPhoneState1 == PackageManager.PERMISSION_GRANTED)) {
                    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
                    Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                    location(location);
                } else {
                    ActivityCompat.requestPermissions("Your Activity Name", new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 2);
                }

  private void location(Location location) {
    if (location != null) {
        location.setLatitude(location.getLatitude());
        location.setLongitude(location.getLongitude());
        final MarkerOptions markerOptions = new MarkerOptions();
        final LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
        markerOptions.position(latLng);
     }
  }

希望这对您有帮助

答案 2 :(得分:0)

locationManager.requestLocationUpdates()是您正在使用的currentLocation = locationManager.getLastKnownLocation(provider);异步调用,它可能尚未收到您的当前位置,因为异步调用可能尚未完成。确保您使用onLocationChanged()回调来获取位置。