gpt4 book ai didi

java - 我需要填写什么才能获得我的位置?就像谷歌的蓝点

转载 作者:搜寻专家 更新时间:2023-11-01 08:27:21 26 4
gpt4 key购买 nike

我刚开始使用 Google Maps API,并且已经在 fragment 中实现了 map 。我在 map 上添加了一个标记。现在我希望我的位置像谷歌地图那样弹出蓝点。谁能帮我完成这个?非常感谢。

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

}
}

list 文件有这个 ACCESS_FINE_LOCATION

最佳答案

您需要启用我的位置图层

  1. 检查您的应用是否获得ACCESS_FINE_LOCATIONACCESS_COARSE_LOCATION

  2. 权限
  3. 启用我的位置层,使用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 中执行此操作:

    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

关于java - 我需要填写什么才能获得我的位置?就像谷歌的蓝点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43500648/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com