Ich mache eine App für eine meiner Klassen, und ich muss Markierungen auf einer Karte anzeigen. Es passieren zwei komische Dinge: Erstens wird onMapReady zweimal ausgeführt und zweitens werden die Marker nicht auf der Karte angezeigt. Dies ist mein Code:GoogleMap in Android zeigt keine Markierungen an
public class MapFragment extends SupportMapFragment implements OnMapReadyCallback {
private GoogleMap mMap;
private Event mEventSelected;
private Person mPersonSelected;
private Map<Marker, Event> mMapMarkers;
private List<Polyline> mLines;
private TextView mPersonFullName;
private TextView mEventDescription;
private LinearLayout mMapEvent;
private ImageView mEventIcon;
private MenuItem searchIcon;
private MenuItem filterIcon;
private MenuItem settingsIcon;
private MenuItem topIcon;
private String EVENT_ID;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
mMapMarkers = new HashMap<>();
mLines = new ArrayList<>();
mEventSelected = null;
mPersonSelected = null;
getMapAsync(this);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
super.onCreateView(inflater, container, savedInstanceState);
View v = inflater.inflate(R.layout.fragment_map, container, false);
drawDefaultLayout(v);
return v;
}
private void pinEvents() {
mMap.clear();
Map <String, Event> events = FamilyMapData.getEvents();
for (Event event : events.values())
{
LatLng coordinates = new LatLng(event.getLatitude(), event.getLongitude());
String description = event.getDescription();
float color = 0;
switch (description){
case "baptism":{
color = BitmapDescriptorFactory.HUE_CYAN;
break;
}
case "birth":{
color = BitmapDescriptorFactory.HUE_BLUE;
break;
}
case "death":{
color = BitmapDescriptorFactory.HUE_AZURE;
break;
}
case "marriage":{
color = BitmapDescriptorFactory.HUE_RED;
break;
}
case "christening":{
color = BitmapDescriptorFactory.HUE_ORANGE;
break;
}
case "census":
{
color = BitmapDescriptorFactory.HUE_GREEN;
break;
}
}
mMapMarkers.put(mMap.addMarker(new MarkerOptions().position(coordinates).icon(BitmapDescriptorFactory.defaultMarker(color))), event);
}
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
pinEvents();
}
ich einige von Ihnen Hoffnung, mir herauszufinden, wären in der Lage zu helfen, warum die Markierungen nicht auf der Karte angezeigt.
Vielen Dank im Voraus,
EDIT
Dies ist das xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/map"
android:layout_width="wrap_content"
android:layout_height="0dp"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_weight="5"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight="1"
android:clickable="true"
android:baselineAligned="false"
android:id="@+id/map_event">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/icon_event_divisor">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="40dp"
android:paddingBottom="40dp"
android:paddingStart="20dp"
android:paddingEnd="20dp"
android:clickable="false"
android:layout_gravity="center"
android:id="@+id/event_icon"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/person_full_name"
android:textSize="30sp"
android:id="@id/person_full_name"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:text="@string/event_description"
android:layout_below="@id/person_full_name"
android:id="@+id/event_description"/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
Vielen Dank für Ihre schnelle Antwort. Ich habe versucht, das zu tun, aber MapFragment ist null, also erhalte ich eine NullPointerException, wenn ich getMapAsync darauf anrufe. –
Kannst du das XML auch posten? –
Fertig, ich hätte es vorher dort hinstellen sollen, danke, dass du mich daran erinnert hast. –