My app call getmap() method and notice that is depracated and I've to use getMapAsync() instead, however I've read all posts everywhere on the internet and none helped me. Sometimes I got "null pointer return message". I'll hope someone help me to do that.....
My MapFragment.java:
private void setUpMapIfNeeded() {
if (map == null) {
map = ((MapView) view.findViewById(R.id.map)).getMap();
if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
map.setMyLocationEnabled(false);
map.getUiSettings().setMyLocationButtonEnabled(false);
map.getUiSettings().setZoomControlsEnabled(false);
btnMyLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Location loc = map.getMyLocation();
if (myLocation != null) {
LatLng latLang = new LatLng(myLocation.getLatitude(),
myLocation.getLongitude());
animateCameraToMarker(latLang, true);
}
}
});
map.setOnCameraChangeListener(new OnCameraChangeListener() {
public void onCameraChange(CameraPosition camPos) {
if (currentZoom == -1) {
currentZoom = camPos.zoom;
} else if (camPos.zoom != currentZoom) {
currentZoom = camPos.zoom;
return;
}
if (!isMapTouched) {
curretLatLng = camPos.target;
if (!isAddDestination) {
// layoutMarker.setVisibility(LinearLayout.VISIBLE);
if (listType.size() > 0) {
stopUpdateProvidersLoaction();
getAllProviders(curretLatLng);
}
getAddressFromLocation(camPos.target, etSource);
}
}
isMapTouched = false;
}
});
}
}
The onCreateView: @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
... ... setUpMapIfNeeded(); ... return view; }
My fragment_map.xml
<FrameLayout
android:id="@+id/mapFrameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:visibility="visible" >
<com.google.android.gms.maps.MapView
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent" />