Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android GoogleMaps setMyLocationEnabled(true)未更新(Genymotion)_Android_Google Maps_Genymotion_Android Permissions_Genymotion Gps - Fatal编程技术网

Android GoogleMaps setMyLocationEnabled(true)未更新(Genymotion)

Android GoogleMaps setMyLocationEnabled(true)未更新(Genymotion),android,google-maps,genymotion,android-permissions,genymotion-gps,Android,Google Maps,Genymotion,Android Permissions,Genymotion Gps,我正在使用新的Android API 23的权限更新我的代码。如果我想显示用户的位置,我只需要map.setMyLocationEnabled(true)。如果我有权限,我可以查看用户的位置,如果我没有权限,我不会进行此API调用,但在请求权限和用户接受授予位置权限后,我无法使用用户的位置更新地图。我尝试了两种方法: 方法1(重建地图): private void populateMap() { mapFragment.getMapAsync(new OnMapReadyCal

我正在使用新的
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;
            }
        }
    }
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;
            }
        }
    }
方法2(使用地图实例并更新它):

 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;
            }
        }
    }
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:当然,如果我杀死我的片段并再次启动它,一切正常,当我必须提示用户获得权限,然后在用户授权后用我的位置更新地图时,就会发生此问题。

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

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();
            }
        }
    }
}

所以,我用
Genymotion
进行测试,因为我无法使用
Android
6.0的设备,但现在,我借用了一个,我在那个物理设备上测试了这段代码,我很高兴地注意到它可以工作。
我猜这是一个
Genymotion
问题。哦,好吧,代码起作用了。

谢谢

嗨!感谢您的输入,但请参阅我的“ps”。如果我杀死这个片段并重新启动它,它会工作,但我想避免这种情况,这就是你的代码所做的,完成并重新启动。我想在不杀死我的碎片的情况下更新地图。谢谢