【问题标题】:GoogleMaps setMyLocationEnabled(true) not updating (Genymotion)GoogleMaps setMyLocationEnabled(true)未更新(Genymotion)
【发布时间】:2016-05-30 15:01:57
【问题描述】:

我正在使用新的Android API 23 的权限更新我的代码。如果我想显示用户的位置,我只需要map.setMyLocationEnabled(true)。如果我有权限,我可以看到用户的位置,如果我没有权限,我不会进行此 API 调用,但是在请求权限并且用户接受授予位置权限后,我无法使用用户的位置更新地图。我尝试了两种方法:

方法 1(重建地图):

 private void populateMap() {
        mapFragment.getMapAsync(new OnMapReadyCallback() {
                @Override
                public void onMapReady(GoogleMap map) {
                    ...
                    if (PermissionsManager.getInstance().hasLocationPermissions(getActivity())) {
                        map.setMyLocationEnabled(true);
                    } else {
                        request permissions...
                    }
                    ...
                }
        }
 }

@Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case PermissionsManager.LOCATION_PERMISSION_REQUEST: {
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    populateMap();
                }
                return;
            }
        }
    }

方法 2(使用地图实例并更新它):

private void populateMap() {
        mapFragment.getMapAsync(new OnMapReadyCallback() {
                @Override
                public void onMapReady(GoogleMap map) {
                    mMap = map;
                    ...
                    if (PermissionsManager.getInstance().hasLocationPermissions(getActivity())) {
                        map.setMyLocationEnabled(true);
                    } else {
                        request permissions...
                    }
                    ...
               }
        }
}

@Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case PermissionsManager.LOCATION_PERMISSION_REQUEST: {
                if (mMap!= null && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    mMap.setMyLocationEnabled(true);
                }
                return;
            }
        }
    }

在这两种方法中,位置按钮都会显示,但带有用户位置的蓝点不会显示。 我在这里想念什么?谢谢。

ps:当然,如果我杀死我的 Fragment 并再次启动它,一切正常,当我必须提示用户授予权限,然后在用户授权后使用我的位置更新地图时,就会发生此问题。

【问题讨论】:

    标签: android google-maps genymotion android-permissions genymotion-gps


    【解决方案1】:

    所以,我使用 Genymotion 进行测试,因为我无法使用带有 Android 6.0 的设备,但现在,我借了一个,我已经在该物理设备上测试了这段代码,我很高兴注意到它有效。
    猜猜这是Genymotion 的问题。哦,好吧,代码正在运行。
    谢谢!

    【讨论】:

      【解决方案2】:

      您能否使用以下代码并将结果反馈给我:-

      import android.Manifest;
      import android.content.pm.PackageManager;
      import android.os.Build;
      import android.support.annotation.NonNull;
      import android.support.v4.app.ActivityCompat;
      import android.support.v4.app.FragmentActivity;
      import android.os.Bundle;
      
      import com.google.android.gms.maps.CameraUpdateFactory;
      import com.google.android.gms.maps.GoogleMap;
      import com.google.android.gms.maps.OnMapReadyCallback;
      import com.google.android.gms.maps.SupportMapFragment;
      import com.google.android.gms.maps.model.LatLng;
      import com.google.android.gms.maps.model.MarkerOptions;
      
      public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
      
          private GoogleMap mMap;
          /**
           * Id to identity READ_CONTACTS permission request.
           */
          private static final int REQUEST_ACCESS_FINE_LOCATION = 0;
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_maps);
              // Obtain the SupportMapFragment and get notified when the map is ready to be used.
              SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                      .findFragmentById(R.id.map);
      
              mapFragment.getMapAsync(this);
          }
      
      
          /**
           * Manipulates the map once available.
           * This callback is triggered when the map is ready to be used.
           * This is where we can add markers or lines, add listeners or move the camera. In this case,
           * we just add a marker near Sydney, Australia.
           * If Google Play services is not installed on the device, the user will be prompted to install
           * it inside the SupportMapFragment. This method will only be triggered once the user has
           * installed Google Play services and returned to the app.
           */
          @Override
          public void onMapReady(GoogleMap googleMap) {
              mMap = googleMap;
              // Sets the map type to be "hybrid"
              mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
              // Add a marker in Sydney and move the camera
              LatLng sydney = new LatLng(-34, 151);
              mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
              mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
      
              if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                  // TODO: Consider calling
                  //    ActivityCompat#requestPermissions
                  // here to request the missing permissions, and then overriding
                  //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                  //                                          int[] grantResults)
                  // to handle the case where the user grants the permission. See the documentation
                  // for ActivityCompat#requestPermissions for more details.
                  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                      requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_ACCESS_FINE_LOCATION);
                  }
                  return;
              }
              mMap.setMyLocationEnabled(true);
          }
      
          /**
           * Callback received when a permissions request has been completed.
           */
          @Override
          public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                                 @NonNull int[] grantResults) {
              if (requestCode == REQUEST_ACCESS_FINE_LOCATION) {
                  if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                      startActivity(getIntent());
                      finish();
                  }
              }
          }
      }
      

      【讨论】:

      • 嗨!感谢您的输入,但请参阅我的“ps”。如果我杀死片段并重新启动它,它可以工作,但我想避免这种情况,这就是你的代码所做的,完成并重新启动。我想在不杀死我的 Fragment 的情况下更新地图。谢谢。
      猜你喜欢
      • 1970-01-01
      • 2016-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多