setMyLocationEnabled(true)显示蓝点但不显示MyLocation按钮

时间:2016-12-04 19:31:08

标签: android android-studio google-maps-android-api-2

setMyLocationEnabled(true)对我没有任何作用。我的地图中显示了当前位置的蓝点,但MyLocation按钮未显示。我已经经历了许多关于此问题的堆栈溢出问题无济于事。以下是我的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ActivityCompat.requestPermissions(GPS.this,
            new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
            1);
    setContentView(R.layout.activity_gps);
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);
    try {
        Log.d("request","done");
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, this);
    }
    catch (SecurityException e) {
        Log.d("asdf", "Asdf");
    }
    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
           fab.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                          Intent i = new Intent(getApplicationContext(), Leaderboard.class);
                          startActivity(i);
                        }
                });
}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    mMap.setMyLocationEnabled(true);
    mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    // Create a criteria object needed to retrieve the provider
    Criteria criteria = new Criteria();
    // Get the name of the best available provider
    String provider = locationManager.getBestProvider(criteria, true);
    String locationProvider = LocationManager.NETWORK_PROVIDER;
    String GPSProvider = LocationManager.GPS_PROVIDER;
    Location myLoc;
    if (provider == null)
        provider = locationProvider;
    // We can use the provider immediately to get the last known location

    try {
        myLoc = locationManager.getLastKnownLocation(provider);
        LatLng loc;
        if (myLoc == null) {
            loc = new LatLng(38.0317, -78.5108);
        } else {
            loc = new LatLng(myLoc.getLatitude(), myLoc.getLongitude());
            Log.d("lat2", "" + myLoc.getLatitude());
            Log.d("long2", "" + myLoc.getLongitude());
        }

        mMap.addMarker(new MarkerOptions().position(loc).title("Your Location"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(loc));
        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(loc, 16.0f));

    } catch (SecurityException e) {
        Log.d("asdf", "Asdf");
    }
}

0 个答案:

没有答案
相关问题