Mein Kommentar in Karten mit Fragmente, zuerst sollten Sie die Ansicht refrenece wie Sie etwas von ihm nennen, ist der Grund, warum ich meine Ansicht
View view = inflater.inflate(R.layout.activity_maps, null, false);
Zweiten Aufruf Kind Fragment Manager this.getChildFragmentManager()
statt getActivity().getSupportFragmentManager()
erste aufblasen
Hier ist die vollständige Beispiel Karte
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class ClinicFragment extends Fragment implements OnMapReadyCallback {
private GoogleMap mMap;
public static ClinicFragment newInstance() {
ClinicFragment fragment = new ClinicFragment();
return fragment;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_maps, null, false);
SupportMapFragment mapFragment = (SupportMapFragment) this.getChildFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
return view;
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
erforderlich anzuzeigen Erlaubnis
<permission
android:name="your.package.name.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="your.package.name.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Plus Merkmalselement-
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
meisten Import Google Maps Schlüssel
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="your_key_here" />
aktualisieren diese Meta-Daten hinzufügen, wenn Sie Error inflating class fragment
Gesicht
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="your_key_here" />
sommerliche für Mainfest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="your.package.name" >
// permission here
<application
android:name=".Activities.MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
// feature element
// maps key
</application>
</manifest>
activity_maps.xml
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.imaadv.leaynik.ClinicFragment" />
Wo wäre ein geeigneter Ort sein, der 'getMapAsync()' dann anrufen? Grundsätzlich hat meine App 3 Fragmente, 1 Aktivität und einen ViewPager, um zwischen diesen Fragmenten zu gleiten. Sollte es die Aktivität onCreate() der Aktivität sein? – Neutrino
@Neutrino: "Grundsätzlich hat meine App 3 Fragmente, 1 Aktivität und einen ViewPager, der zwischen diesen Fragmenten verschoben werden kann" - dafür brauchst du keine verschachtelten Fragmente. Ihre Aktivität hat einen 'ViewPager'. Ihr 'PagerAdapter' erzeugt drei Fragmente. Es ist keine Verschachtelung erforderlich. Lassen Sie den 'FragmentPagerAdapter' direkt eine neue Instanz von' SupportMapFragment' erstellen; Sie müssen ein Layout nicht aufblasen.Dieses Beispiel zeigt einen 'ViewPager' mit zehn Kartenfragmenten: https://github.com/commonsguy/cw-omnibus/tree/master/MapsV2/Pager – CommonsWare
Sorry, ich bin irgendwie verloren. Meine 'ViewPagerAdapter's' getItem() 'überprüft die Position und ruft in diesem Fall' return new Example() 'auf - ich schätze, dass das Aufblasen des Layouts in diesem Fall erforderlich ist? Und dann bin ich mir nicht so sicher, wo ich meine Operationen machen soll: in Fragmenten Code, Aktivitätencode oder anderswo. – Neutrino