java - 谷歌标记不显示
问题描述
在下面的代码中不显示标记。我用预览版 android studio 版本制作的代码,跟踪没有问题,但现在有。我想用跟踪标签制作谷歌地图,因此我认为可以' t 使用活动,但只使用片段。任何人都知道如何做到这一点?
public class Tab2Map extends Fragment implements OnMapReadyCallback {
GoogleMap mGoogleMap;
MapView mMapView;
View mView;
public Tab2Map() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.fragment_tab2, container, false);
return mView;
}
@Override
public void onViewCreated(View view,Bundle savedInstanceState){
super.onViewCreated(view,savedInstanceState);
mMapView=(MapView) mView.findViewById(R.id.map);
if(mMapView != null){
mMapView.onCreate(null);
mMapView.onResume();
mMapView.getMapAsync(this);
}
}
@Override
public void onMapReady(GoogleMap googleMap) {
MapsInitializer.initialize(getContext());
mGoogleMap = googleMap;
if (ActivityCompat.checkSelfPermission(this.getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this.getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
mGoogleMap.setMyLocationEnabled(true);
LocationManager locationManager = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
LatLng latlng = new LatLng(location.getLatitude(), location.getLongitude());
mGoogleMap.addMarker(new MarkerOptions().position(latlng).title("Current Location").snippet(latlng.toString()));
CameraPosition position=CameraPosition.builder().target(latlng).zoom(16).bearing(0).build();
mGoogleMap.moveCamera(CameraUpdateFactory.newCameraPosition(position));
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5, 15, locationListener);
}
}
fragment_tab2.xml
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Tab2Map">
<com.google.android.gms.maps.MapView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map"/>
</android.support.constraint.ConstraintLayout>
解决方案
推荐阅读
- eclipse - Tycho 无法构建客户端产品,自定义功能存在问题
- jenkins - java.lang.IllegalArgumentException:并行执行作业时需要命名参数
- python - 电报机器人 URL 返回 404
- node.js - 为什么 npm 不能解析“^7.0.35”和“^7.0.0 || ^8.0.1”?
- r - 如何将具有多重变量的 POSIXct 数据转换为时间序列格式
- javascript - 如何将 mongodb findone 值保存到 javascript 变量中,以便稍后在 nodejs 上使用它
- python-3.x - 如何在另一个上增加 .png 图像?
- drupal - Drupal 9 权限问题
- javascript - NestJS 拦截器 - 将数据附加到传入请求的标头或正文
- java - 字符串索引超出范围:22 异常 Java