在我的带有导航抽屉的android应用中,我遇到了一个片段问题,其中使用了mapview:当我第一次启动该片段时,它仅显示我要标记的一个列表中的第一个画。我必须重新加载片段以查看地图上所有标记的绘制(例如,单击菜单上的另一个声音,然后再次单击地图片段)。我感觉该片段会在绘制所有标记之前启动地图。可能是问题吗?我该如何解决?这是我的代码:
public class MapFragment extends Fragment implements OnMapReadyCallback, GoogleMap.OnInfoWindowClickListener {
private GoogleMap mGoogleMap;
private MapView mMapView;
private View mView;
public MapFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
mView = inflater.inflate(R.layout.map_fragment, container, false);
return mView;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mMapView = mView.findViewById(R.id.map);
if (mMapView != null) {
mMapView.onCreate(null);
mMapView.onResume();
mMapView.getMapAsync(this);
}
}
@Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;
drawMarkerOnTheMap(mGoogleMap);
mGoogleMap.getUiSettings().setZoomControlsEnabled(true);
mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
LatLng city = new LatLng(40.679425, 14.755968);
CameraPosition camera = CameraPosition.builder().target(city).zoom(15).build();
mGoogleMap.moveCamera(CameraUpdateFactory.newCameraPosition(camera));
}
private void drawMarkerOnTheMap(GoogleMap googleMap) {
try {
InputStream is = getContext().getAssets().open("turisticpoint.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(is);
Element element=doc.getDocumentElement();
element.normalize();
NodeList nList = doc.getElementsByTagName("turisticpoint");
for (int i=0; i<nList.getLength(); i++) {
Node node = nList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element2 = (Element) node;
String title = getValue("title", element2);
String tit = getContext().getResources().getString(getContext().getResources().getIdentifier(title, "string", getActivity().getPackageName()));
Double latitude = Double.valueOf(getValue("latitude", element2));
Double longitude= Double.valueOf(getValue("longitude", element2));
LatLng position = new LatLng(latitude, longitude);
MarkerOptions marker = new MarkerOptions().
position(position).
title(tit)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE));
googleMap.addMarker(marker);
}
}
} catch (Exception e) {e.printStackTrace();}
}
private static String getValue(String tag, Element element) {
NodeList nodeList = element.getElementsByTagName(tag).item(0).getChildNodes();
Node node = nodeList.item(0);
return node.getNodeValue();
}
}
在我的logcat中,我刚发现此错误:
E/libprocessgroup: failed to make and chown /acct/uid_10063: Read-only file system
这是我的清单文件:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="package">
<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:xlargeScreens="true" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".HomeActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
<activity
android:name=".MainActivity"
android:label="MyApp"
android:theme="@style/AppTheme.NoActionBar"></activity>
</application>
</manifest>
和我的build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "myappId"
minSdkVersion 15
targetSdkVersion 28
multiDexEnabled true
versionCode 16
versionName "1.5"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
missingDimensionStrategy 'minApi', 'minApi18', 'minApi23'
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:support-v4:28.0.0'
implementation 'com.android.support:design:28.0.0'
implementation 'com.google.android.gms:play-services-maps:16.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
这是一个类似于我的xml文件:
<records>
<turisticpoint>
<titolo>Gardens</titolo>
<latitudine>40.678480</latitudine>
<longitudine>14.753152</longitudine>
</turisticpoint>
<turisticpoint>
<titolo>Castle</titolo>
<latitudine>40.6794731</latitudine>
<longitudine>14.7544442</longitudine>
</turisticpoint>
<turisticpoint>
<titolo>Beach</titolo>
<latitudine>40.6965378</latitudine>
<longitudine>14.7777038</longitudine>
</turisticpoint>
</records>
我总共有13个旅游点。