我正在努力使用谷歌地图。我正在使用带有碎片的抽屉,所以我必须使用MapView afaik(没有片段调用片段)。
首先我有授权失败,所以我重新生成了一个密钥,运行了keytool,我的google_maps_api.xml是正确的,因此有关授权问题的消息消失了。我仍然看不到任何内容,我有一个关于StrictModeDiskReadViolation的奇怪消息。
这是我在logcat中得到的:
12-15 20:41:14.842 703-703/com.bernard_zelmans.check I/Google Maps Android API: Google Play services package version: 10084448
12-15 20:41:14.842 703-703/com.bernard_zelmans.check W/f: Suppressed StrictMode policy violation: StrictModeDiskReadViolation
12-15 20:41:14.842 703-703/com.bernard_zelmans.check W/f: Suppressed StrictMode policy violation: StrictModeDiskWriteViolation
12-15 20:41:14.882 703-703/com.bernard_zelmans.check W/f: Suppressed StrictMode policy violation: StrictModeDiskReadViolation
12-15 20:41:14.882 703-25272/com.bernard_zelmans.check W/f: Suppressed StrictMode policy violation: StrictModeDiskReadViolation
12-15 20:41:14.882 703-703/com.bernard_zelmans.check W/f: Suppressed StrictMode policy violation: StrictModeDiskWriteViolation
12-15 20:41:14.882 703-25274/com.bernard_zelmans.check W/f: Suppressed StrictMode policy violation: StrictModeDiskReadViolation
这是我的应用程序的快照:
这是我的代码:
public class PortFragment extends Fragment implements OnMapReadyCallback {
Context context;
View view;
MapView mapView;
GoogleMap map;
LatLng CENTER = null;
public LocationManager locationManager;
private GoogleMap googleMap;
double longitude = 0;
double latitude = 0;
public PortFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.portscan, null);
MapsInitializer.initialize(this.getActivity());
mapView = (MapView) view.findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);
return (view);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
context = getActivity().getApplicationContext();
}
@Override
public void onMapReady(GoogleMap map) {
setUpMap();
googleMap = map;
LatLng sydney = new LatLng(-34, 151);
googleMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
public void setUpMap() {
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
googleMap.setMyLocationEnabled(true);
googleMap.setTrafficEnabled(true);
googleMap.setIndoorEnabled(true);
}
下面是我的manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bernard_zelmans.check">
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.STORAGE" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:configChanges="keyboardHidden|screenSize"
android:icon="@mipmap/ic_launcher"
android:label="Test"
android:logo="@drawable/ic_launcher"
android:screenOrientation="portrait"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Activities.Settings" />
<activity android:name=".Activities.About" />
<service
android:name=".Service"
android:exported="false" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
</application>
</manifest>
答案 0 :(得分:1)
如果您想在片段中添加地图,则不应添加像这样的创建方式。您应该像在布局中一样在布局中添加它,但是在onCreateView()方法中,您应该获取视图并直接在其上调用getMapAsync:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.portscan, container);
MapFragment mapFragment = (MapFragment) getChildFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
return (view);
}
在布局中:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:name="com.google.android.gms.maps.MapFragment"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
如您所见,您首先在布局中添加地图片段,然后在您自己的片段中处理它。
你应该看一下android gmap文档:
https://developers.google.com/maps/documentation/android-api/map