我正在开发一个Android应用程序,需要将Google地图加载到片段中。 地图仅部分加载。地图的一半仍然是灰色的,不会加载。 请提供您的建议。我不确定这个问题。
以下是项目文件:
MapFragment.java:
public class MapFragment extends Fragment {
MapView mMapView;
GoogleMap googleMap;
Context context;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// inflat 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();
}
context = getContext();
DatabaseHelper databaseHelper = new DatabaseHelper(context, null, null, 0);
ArrayList<PropertyDetails> arrayList = databaseHelper.readData();
Log.d("PAVAN", "Details "+arrayList.get(0).getCity());
Log.d("PAVAN", "Details "+arrayList.get(0).getLatitude());
googleMap = mMapView.getMap();
for(int i = 0 ; i < arrayList.size() ; i++) {
// latitude and longitude
String PropertyType = arrayList.get(i).PropertyType;
String Address = arrayList.get(i).Address;
String City = arrayList.get(i).City;
Double LoanAmount = arrayList.get(i).LoanAmount;
Double APR = arrayList.get(i).APR;
Double MonthlyPayment = arrayList.get(i).MonthlyPayment;
Double Latitude = arrayList.get(i).getLatitude();
Double Longitude = arrayList.get(i).getLongitude();
// create marker
MarkerOptions marker = new MarkerOptions().position(
new LatLng(Latitude, Longitude)).title(PropertyType + "<br/>" + Address + "<br/>" +
City + '\n' + LoanAmount + '\n' + APR + "<br/>" + MonthlyPayment).snippet("" + APR).snippet("" + MonthlyPayment);
// Changing marker icon
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_ROSE));
// adding marker
googleMap.addMarker(marker);
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(17.385044, 78.486671)).zoom(12).build();
// googleMap.setInfoWindowAdapter(CustomWindowAdapter);
googleMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
}
// Perform any camera updates here
return v;
}
@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();
}
}
fragment_map.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="8dp"
tools:context="com.example.pavanshah.mortgagecalculator.MainActivity$PlaceholderFragment">
<com.google.android.gms.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
清单文件:
<?xml version="1.0" encoding="utf-8"?>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
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.gms.version"
android:value="@integer/google_play_services_version">
</meta-data>
<meta-data android:name="com.google.android.geo.API_KEY"
android:value="My_Key">
</meta-data>
</application>