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
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!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.