Reputation: 988
I'm struggling with Google Maps. I'm using a drawer with fragments so I have to use MapView afaik (no fragment call from a fragment).
First I had an authorization failure, so I re-generated a key, ran keytool, my google_maps_api.xml is correct so the message about the authorization issue is gone. Still I see no content and I have a strange message about StrictModeDiskReadViolation.
this is what I get in 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
And here is a snapshot from my app:
Here is my code:
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);
}
Below is my 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>
Upvotes: 0
Views: 649
Reputation: 1272
If you want to add a map in your fragment you should not add create it like this. You should add it in your layout, as you did, but then in onCreateView() method you should get the view and directly call getMapAsync on it :
@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);
}
And in layout :
<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"/>
As you see, you first add map fragment in layout and then you handle it in your own fragment.
You should take a look at android gmap documentation here :
https://developers.google.com/maps/documentation/android-api/map
Upvotes: 1