簡體   English   中英

要填寫我的位置我需要填寫什么? 就像Google的藍點一樣

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

我是使用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. 檢查您的應用是否獲得權限ACCESS_FINE_LOCATIONACCESS_COARSE_LOCATION

  2. 啟用“ 我的位置”圖層 ,使用mMap.setMyLocationEnabled(true);

如果不執行步驟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執行此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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM