ここでソリューションを使用してフラグメント内のマップにマーカーを追加していますが、マーカーを1つしか追加できない理由がわかりません。
テストコードで2回呼び出されるカメラ位置を作成するときに、何かが足りないと確信しています。(以下のaddLocationToMapを参照してください)。
メインスレッドで地理座標のArrayListを取得していて、各エントリでaddLocationToMapを呼び出してマップタブのリストをループしたいと思います。
これが私のコードです:
public class RentalMapFragment extends Fragment {
MapView mMapView;
private GoogleMap googleMap;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// inflate and return the layout
View v = inflater.inflate(R.layout.fragment_map, container, false);
mMapView = (MapView) v.findViewById(R.id.mapView);
mMapView.onCreate(savedInstanceState);
mMapView.onResume();// needed to get the map to display immediately
try {
MapsInitializer.initialize(getActivity().getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}
googleMap = mMapView.getMap();
int level = 12;
addLocationToMap(17.385044, 78.486679, "Marker 2", level);
addLocationToMap(17.385044, 78.486671, "Marker 1", level);
// Perform any camera updates here
CameraPosition cameraPosition = new CameraPosition.Builder().target(new LatLng(17.385000, 78.486600)).zoom(level).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
return v;
}
private void addLocationToMap(double latitude, double longitude, String markerTitle, int level) {
// Create lat/lon
LatLng latLng = new LatLng(latitude, longitude);
// create marker
MarkerOptions marker = new MarkerOptions().position(latLng).title(markerTitle);
// Changing marker icon
marker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ROSE));
// adding marker
googleMap.addMarker(marker);
}
@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();
}
}
各マーカーを配置した後、カメラの位置を作成するべきではないことがわかりました。むしろ、すべてのマーカーを追加した後、マーカーを表示するカメラの位置を選択する必要があります。
これを実証するために質問を更新しています。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加