Android开发,蓝点,我的位置按钮和实际位置不起作用

时间:2019-06-27 14:17:24

标签: android google-maps maps

我目前正在使用Android开发来探索应用程序中的基本地图开发。我可以在地图视图中看到Googleplex上的android仿真器提供的默认位置,但是没有蓝点,并且“我的位置”按钮似乎不起作用

我想知道

  1. 如何按照正常的Google地图显示的蓝点表示我的当前位置

  2. 我如何使用模拟器的实际位置来精确跟踪地图并测试我的“尚需代码”功能。

通过我在互联网上解决蓝点问题的研究,我发现了诸如

 googleMap.setMyLocationEnabled(true);
 googleMap.getUiSettings().setMyLocationButtonEnabled(true);
 googleMap.getUiSettings().setAllGesturesEnabled(true);
 googleMap.getUiSettings().setZoomControlsEnabled(true); 

但是它也不起作用。

感谢您的时间,并希望有一些方向。

我当前的地图活动代码如下

public class MapActivity extends AppCompatActivity implements OnMapReadyCallback {

@Override
public void onMapReady(GoogleMap googleMap) {
    Toast.makeText(this, "Map is Ready", Toast.LENGTH_SHORT).show();
    Log.d(TAG, "onMapReady: map is ready");
    mMap = googleMap;

    if (mLocationPermissionsGranted) {
        getDeviceLocation();

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

            return;
        }
        mMap.setMyLocationEnabled(true);
        googleMap.getUiSettings().setMyLocationButtonEnabled(true);
        googleMap.getUiSettings().setAllGesturesEnabled(true);
        googleMap.getUiSettings().setZoomControlsEnabled(true);
        //LatLng latlng = new LatLng(location)

    }
}
private static final String TAG = "MapActivity";
private static final String FINE_LOCATION = Manifest.permission.ACCESS_FINE_LOCATION;
private static final String COARSE_LOCATION = Manifest.permission.ACCESS_COARSE_LOCATION;
private static final int LOCATION_PERMISSION_REQUEST_CODE = 1234;
private static final float DEFAULT_ZOOM = 15f;
//variables
private Boolean mLocationPermissionsGranted = false;
private GoogleMap mMap;
private FusedLocationProviderClient mFusedLocationProviderClient;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map);

    getLocationPermission();
}

private void initMap(){
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
    //prepare map
    mapFragment.getMapAsync(MapActivity.this);
    }

private void getDeviceLocation(){
    Log.d(TAG,"getDeviceLocation: getting the current devices location" );
    mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
    try{
        Task location = mFusedLocationProviderClient.getLastLocation();
        if(location == null)
        {

        }
        location.addOnCompleteListener(new OnCompleteListener() {
            @Override
            public void onComplete(@NonNull Task task) {
                if (task.isSuccessful()){
                    Log.d(TAG,"onComplete: found location!");
                    Location currentLocation = (Location)task.getResult();
                    moveCamera(new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude()), DEFAULT_ZOOM);

                }else{
                    Log.d(TAG, "onComplete: current location is null");
                    Toast.makeText(MapActivity.this, "unable to get current location", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }catch(SecurityException e){
        Log.e(TAG, "getDeviceLocation: Security Exception: " + e.getMessage());
    }
}

private void moveCamera(LatLng latLng, float zoom){
    Log.d(TAG, "moveCamera:moving the camera to: lat: " + latLng.latitude + ", lng: " +latLng.longitude);
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng,zoom));
}
private void getLocationPermission(){
    Log.d(TAG, "getLocationPermission: getting location permissions");
    String[] permissions = {Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION};

    if (ContextCompat.checkSelfPermission(this.getApplicationContext(),
            FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){
        if (ContextCompat.checkSelfPermission(this.getApplicationContext(),
                COARSE_LOCATION)== PackageManager.PERMISSION_GRANTED){
            mLocationPermissionsGranted = true;
            initMap();
        }else{
            ActivityCompat.requestPermissions(this, permissions, LOCATION_PERMISSION_REQUEST_CODE);

        }
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    Log.d(TAG, "onRequestPermissionsResult: called.");
    mLocationPermissionsGranted = false;

    switch (requestCode){
        case LOCATION_PERMISSION_REQUEST_CODE:{
            if (grantResults.length > 0 ){
                for (int i =0; i < grantResults.length; i++){
                    if (grantResults[i] !=PackageManager.PERMISSION_GRANTED){
                        mLocationPermissionsGranted = false;
                        Log.d(TAG,"onRequestPermissionsResult: permission failed");
                        return;
                    }
                }
                Log.d(TAG,"onRequestPermissionsResult: permission granted");
                mLocationPermissionsGranted = true;
                //initializing of map
                initMap();
            }
        }
    }
}

}

提出建议,慢慢来!

0 个答案:

没有答案
相关问题