GoogleMaps setMyLocationEnabled(true)不更新(Genymotion)

时间:2016-02-18 12:31:57

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

我正在使用新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并再次启动它一切正常,当我必须提示用户获取权限然后在用户授权后使用我的位置更新地图时会发生此问题。

2 个答案:

答案 0 :(得分:1)

所以,我正在使用@model int <a href="#link1" data-loaded="true" id="btn-link1">Show Info</a> <a href="#link2" data-loaded="false" id="btn-link2">Show Income</a> .... @Html.Hidden("TypeOfContent", "Temp"); @using (Html.BeginForm("ExportData", "MyController", new { CompanyID = Model, typeOfContent = ???? }, FormMethod.Post)) { <div id="MyGrid"> @{Html.RenderAction("MyPartial", "MyController", new { CompanyID = Model});} </div> } .... <script> $(function () { bindCompanyInfo(); $('#btn-link1').on('click', function (e) { bindCompanyInfo(); $('#TypeOfContent').val("CompanyInfo"); }) $('#btn-link2').on('click', function (e) { bindCompanyIncome(); $('#TypeOfContent').val("CompanyIncome"); }) function bindCompanyInfo() { ... }; } function bindCompanyIncome() { ... }; } 进行测试,因为我无法使用Genymotion 6.0的设备,但现在,我已经借了一个,我已经测试了这个代码物理设备,我很高兴地注意到它的工作原理 猜猜这是一个Android问题。哦,代码正在工作。
谢谢!

答案 1 :(得分:0)

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

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