1

I have a Fragment class which contains a MapView.

It is working fine, the only problem is it doesn't show the Current location button, i set it true:

 nMap = mapView.getMap();
nMap.setMyLocationEnabled(true);
nMap.getUiSettings().setZoomControlsEnabled(true);

and i add permissions inside my Manifest xml file:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>

but it hasn't resolved the problem.

mapView = (MapView) rootView.findViewById(R.id.map);
mapView.onCreate(savedInstanceState); 
if (mapView != null) { 
    nMap = mapView.getMap(); 
    nMap.setMyLocationEnabled(true); 
    nMap.getUiSettings().setZoomControlsEnabled(true); 
    mClusterManager = new ClusterManager<>(this.getActivity(), nMap); 
    nMap.setOnCameraChangeListener(mClusterManager); 
    nMap.setOnMarkerClickListener(mClusterManager); 
}

Update 1:

This is very strange i implemented this code:

public class MainFragment extends Fragment implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener {

//Define Variables that necessary for app
CameraUpdate update;
JSONArray array;
ClusterManager<ItemCluster> mClusterManager;
View rootView;
ItemCluster offsetItem;
MapView mapView;
GoogleMap nMap;
GoogleApiClient mLocationClient;

/*
the Constructor of Main Fragment that get the JSON array as input from AsyncTask
which download the JSON file from Meteoapps website
 */
@SuppressLint("ValidFragment")
public MainFragment(JSONArray input) {
    array = input;
}

//----Default constructor
public MainFragment() {
}


// Create the view when the fragment initialized
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_main, container, false);
    mapView = (MapView) rootView.findViewById(R.id.map);
    mapView.onCreate(savedInstanceState);
    mapView.getMapAsync(this);
    return rootView;
}

@Override
public void onMapReady(GoogleMap googleMap) {

    if (mapView != null) {
        nMap = googleMap;
        nMap.setMyLocationEnabled(true);
        nMap.getUiSettings().setZoomControlsEnabled(true);
        mClusterManager = new ClusterManager<>(this.getActivity(), nMap);
        mClusterManager.setRenderer(new OwnIconRendered(getActivity().getApplicationContext(), nMap, mClusterManager));
        nMap.setOnCameraChangeListener(mClusterManager);
        nMap.setInfoWindowAdapter(mClusterManager.getMarkerManager());
        nMap.setOnMarkerClickListener(mClusterManager);
        mLocationClient = new GoogleApiClient.Builder(getActivity())
                .addApi(LocationServices.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();
        mLocationClient.connect();
        Location currentlocation = LocationServices.FusedLocationApi
                    .getLastLocation(mLocationClient);

    }

i know the getLastLocation may cause problem but in this case not because i tried the code on another code and it works, then when i try to log out currentlocation.getLatitude() i face this error:

System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference

Blacksword
  • 331
  • 3
  • 17
  • Could you elaborate the changes that you made? – Sufian Sep 13 '16 at 13:13
  • @Sufian i was using like this: `mapView = (MapView) rootView.findViewById(R.id.map); mapView.onCreate(savedInstanceState); if (mapView != null) { nMap = mapView.getMap(); nMap.setMyLocationEnabled(true); nMap.getUiSettings().setZoomControlsEnabled(true); mClusterManager = new ClusterManager<>(this.getActivity(), nMap); nMap.setOnCameraChangeListener(mClusterManager); nMap.setOnMarkerClickListener(mClusterManager); }` – Blacksword Sep 13 '16 at 13:32
  • If `OnMapReadyCallback` is only there on API 24, then it will never be called on API 23. What was version of Android on the device/emulator you tested on? – Sufian Sep 13 '16 at 14:03
  • @Sufian i update the question. – Blacksword Sep 14 '16 at 06:10

1 Answers1

0

getMap() is deprecated, use getMapAsync(this) instead. Also, implement a OnMapReadyCallback, on the onMapReady() method, set your map configurations there. Other than that, I see no issues with how you configured the Map. Hopefully this will help resolve the issue.

Happy coding!

adjuremods
  • 2,938
  • 2
  • 12
  • 17
  • hi, i tried that out also nothing happened it is really strange i tried another demo app and it worked, but the different was he did everything in MainActivity not in a fragment so i'm not able to know what is wrong with my codes – Blacksword Sep 14 '16 at 10:34