我需要填写什么来获取我的位置?像蓝点谷歌有

时间:2017-04-19 15:57:22

标签: java android google-maps

我不熟悉使用Google Maps API并在片段中实现了地图。我在地图上添加了一个标记。现在我希望我的位置像谷歌地图一样弹出,蓝点。任何人都可以帮我完成这个吗?非常感谢。

我的java文件:

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

}
}

清单文件具有此ACCESS_FINE_LOCATION

1 个答案:

答案 0 :(得分:3)

您需要启用我的位置图层

  1. 检查您的应用获取权限 ACCESS_FINE_LOCATION ACCESS_COARSE_LOCATION

  2. 启用我的位置图层,使用 mMap.setMyLocationEnabled(true);

  3. 如果您不执行第1步,则会获得SecurityException。你应该覆盖ActivityCompat.OnRequestPermissionsResultCallback所以最终的代码应该是这样的:

    // 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;
            }
        }
    }
    

    并在onMapReady中执行此操作:

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

    以下是参考资料:

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

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