Reputation: 2994
Im attempting to display my GPS position on Google maps & also position of others as data is made available.
Here is my fragment that loads the map fragment:
package xyz.fragments;
import android.app.Activity;
import android.app.FragmentManager;
import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.annotation.NonNull;
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;
import xyzR;
public class IncidentMapFragment extends SupportMapFragment implements OnMapReadyCallback {
public static IncidentMapFragment newInstance()
{
return new IncidentMapFragment();
}
private SupportMapFragment mapFragment;
private static GoogleMap map;
public static final int UPDATE_MY_CURRENT_LOCATION = 0;
public static final int UPDATE_MY_TEAMS_CURRENT_LOCATIONS = 1;
public static final int UPDATE_ALL_TEAMS_CURRENT_LOCATIONS = 2;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.incident_map_fragment, container, false);
//I ALSO tried getFragmentManager()
mapFragment = (SupportMapFragment)getChildFragmentManager()
.findFragmentById(R.id.incident_map);
//THE NULLPOINTER when accessing mapFragment IS AT THIS LINE
mapFragment.getMapAsync(this);
return rootView;
}
public static final Handler updateIncidentMap = new Handler(Looper.getMainLooper()) {
public void handleMessage(Message msg) {
final int what = msg.what;
switch(what) {
case UPDATE_MY_CURRENT_LOCATION:
Location myCurrentLocation = (Location) msg.obj;
if (map!=null && myCurrentLocation!=null) {
myKeyOntheMap(map,myCurrentLocation);
}
break;
case UPDATE_MY_TEAMS_CURRENT_LOCATIONS:
break;
case UPDATE_ALL_TEAMS_CURRENT_LOCATIONS:
break;
default: break;
}
}
};
@Override
public void onMapReady(GoogleMap googleMap) {
map = googleMap;
}
private static void myKeyOntheMap( GoogleMap map,Location location)
{
LatLng myPos = new LatLng(location.getLatitude(), location.getLongitude());
map.addMarker(new MarkerOptions().position(myPos)
.title("You"));
map.moveCamera(CameraUpdateFactory.newLatLng(myPos));
}
}
incident_map_fragment:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/incident_map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</ScrollView>
Im getting: Android GoogleMap java.lang.NullPointerException: Attempt to invoke interface method 'void com.google.maps.api.android.lib6.impl.bo.v()' at code line mapFragment.getMapAsync(this);
Any suggestions?
Thanks
Upvotes: 2
Views: 8118
Reputation: 1083
In fragment the SupportMapFragment
not working... to solved this problem Use MapView
..
for use MapView
in fragment
see this answer...
https://stackoverflow.com/a/19354359/9868485
Upvotes: 1