gpt4 book ai didi

android - 标记聚类在 Android 中不显示标记?

转载 作者:行者123 更新时间:2023-11-30 00:30:11 26 4
gpt4 key购买 nike

我在 Android 的 PagerFragment 中设置了一个 MapView。我遵循了 Google Example,但使用 V2 API 设置了一个带有集群标记的 map ,它向我显示了一个没有标记的黑色 map 。我哪里出错了?

import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.LatLng;
import com.google.maps.android.clustering.ClusterManager;

import MapUtils.CustomMarkerItem;

public class TabFragment2 extends Fragment implements OnMapReadyCallback {

MapView mMapView;
private GoogleMap googleMap;

private ClusterManager<CustomMarkerItem> mClusterManager;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.tab_fragment_2, container, false);

mMapView = (MapView) rootView.findViewById(R.id.mapView);
mMapView.onCreate(savedInstanceState);

mMapView.getMapAsync(TabFragment2.this);

return rootView;
}

@Override
public void onResume() {
super.onResume();
mMapView.onResume();
}

@Override
public void onPause() {
super.onPause();
mMapView.onPause();
}

@Override
public void onDestroy() {
super.onDestroy();
mMapView.onDestroy();
}

@Override
public void onLowMemory() {
super.onLowMemory();
mMapView.onLowMemory();
}

@Override
public void onMapReady(GoogleMap googleMap) {
this.googleMap = googleMap;
if (ActivityCompat.checkSelfPermission(getContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
this.googleMap.setMyLocationEnabled(true);


setUpClusterer();
}

private void setUpClusterer() {
// Position the map.
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(51.503186, -0.126446), 10));

// Initialize the manager with the context and the map.
// (Activity extends context, so we can pass 'this' in the constructor.)
mClusterManager = new ClusterManager<>(getContext(), googleMap);
googleMap.setOnMarkerClickListener(mClusterManager);
googleMap.setOnCameraIdleListener(mClusterManager);
// Add cluster items (markers) to the cluster manager.
addItems();
}

private void addItems() {

// Set some lat/lng coordinates to start with.
double lat = 51.5145160;
double lng = -0.1270060;


// Set the title and snippet strings.
String title = "This is the title";
String snippet = "and this would be it's explaination.";

// Add ten cluster items in close proximity, for purposes of this example.
for (int i = 0; i < 10; i++) {
double offset = i / 60d;
lat = lat + offset;
lng = lng + offset;
CustomMarkerItem offsetItem = new CustomMarkerItem(lat, lng, title, snippet);
mClusterManager.addItem(offsetItem);
}
}
}

最佳答案

问题是您将错误的上下文传递给集群实例,即 getContext() 应该是 getActivity()

初始化实例时使用getActivity()

mClusterManager = new ClusterManager<>(getActivity(), googleMap);

当您在 fragment 类中传递上下文时,基本上使用 getActivity

if (ActivityCompat.checkSelfPermission(getActivity(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

关于android - 标记聚类在 Android 中不显示标记?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44573994/

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