简体   繁体   中英

What to I need to fill to get my location? Like the blue dot google has

I am new to using Google Maps API and have implemented the map in the fragment. I have added a marker that on the map. Now i want my location to pop up like google maps have, the blue dot. Can anyone help me finish this? Thanks alot.

My java file:

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.GoogleMap.OnMyLocationChangeListener;
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;

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


@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    // Add a marker in Sydney and move the camera
    LatLng some = new LatLng(80.153, 15.2620);
    mMap.addMarker(new MarkerOptions().position(some).title("Marker in some"));
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(some, 18));

}
}

Manifest file have this ACCESS_FINE_LOCATION

You need to enable My Location Layer .

  1. Check your app get the Permission ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION

  2. Enable the My Location layer , use mMap.setMyLocationEnabled(true);

If you don't do step 1, you would get SecurityException. You should override ActivityCompat.OnRequestPermissionsResultCallback So the final code should look like this:

// Check for permission to access Location
private boolean checkLocationPermission() {
    Log.d(TAG, "checkPermission()");
    // Ask for permission if it wasn't granted yet
    return (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
            == PackageManager.PERMISSION_GRANTED);
}

// Asks for permissions
private void askPermission() {
    Log.d(TAG, "askPermission()");
    ActivityCompat.requestPermissions(
            this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
            PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION
    );
}

// Reaction to whether user granted or denied permissions
@Override
public void onRequestPermissionsResult(
        int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    Log.d(TAG, "onRequestPermissionsResult()");
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode) {
        case PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION: {
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // Permission granted
                if (checkLocationPermission())
                    mMap.setMyLocationEnabled(true);
            } else {
                // Permission denied
                // TODO
            }
            break;
        }
    }
}

and do this in onMapReady :

    if (checkLocationPermission())
        mMap.setMyLocationEnabled(true);
    else
        askPermission();

Here is the reference:

https://developers.google.com/maps/documentation/android-api/location#my-location

https://developers.google.com/maps/documentation/android-api/location#runtime-permission

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM