• 3

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191


File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

Screenshot of the app

I am trying to implement a MapView on a tab in my app, but having issues implementing it as a fragment rather than an activity. I have managed to display the location of the user but having issues as the onLocationChanged method doesn't seem to be? called. Would it be easier to implement as an activity as one of the tabs and use fragments for the others?

public class LocationFragment extends Fragment implements
    LocationListener {

private GoogleMap mGoogleMap;
private MapView mMapView;
private View mView;
private GoogleApiClient apiClient;
private LocationRequest locationRequest;
private LocationManager locationManager;
private Location lastKnowLocation;
private FragmentActivity myContext;
private Marker currentLocationMarker;
final int userRequestCode = 1;

public LocationFragment() {


public void onCreate(Bundle savedInstanceState) {


public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    mView = inflater.inflate(R.layout.fragment_location, container, false);
    return mView;

public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    mMapView = mView.findViewById(R.id.map);
    if (mMapView != null) {

public void onLocationChanged(Location location) {

    lastKnowLocation = location;
    if (currentLocationMarker != null) {

    LatLng currentLatLng = new LatLng(location.getLatitude(), location.getLongitude());

    MarkerOptions markerOptions = new MarkerOptions();
    markerOptions.title("Current Location");
    currentLocationMarker = mGoogleMap.addMarker(markerOptions);


    locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);

    if (apiClient != null) {

        if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, (android.location.LocationListener) LocationFragment.this);


public void onConnected(@Nullable Bundle bundle) {

    locationRequest = new LocationRequest();

    locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);

    if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {

        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0, (android.location.LocationListener) LocationFragment.this);

    • I can see the map in the current implementation using 5 fragments for 5 tabs, but I am having difficulty using the LocationListeners as most of the tutorials online assume you're using an activity rather than fragment. Was wondering if using an activity instead would make life easier
    • Maybe but if you're using a BottomNavigationBar, you should use it to switch between fragments. Post some code and we can have a look at why the listeners aren't working.

You need to have one Activity which includes a frame layout that will be used as the stage to bring up the 5 different fragments. If you dont get it let me know to post some code.

Update :

public class MapFragment extends Fragment {


public static MapFragment newInstance(int i) {
    MapFragment f = new MapFragment();
    return f;

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                         @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.map_fragment, container, false);

    return view;

public void onActivityCreated(@Nullable Bundle savedInstanceState) {

  • 1
Reply Report
    • Yeah I think I already have this, my problem is I'm having difficulty implementing the current location feature in a fragment as most of the documentation refers to activities