android (Support)MapFragment getMap() return null - android

I allready read a lot topics about this problem but nothing helped.
So I want to operate on a GoogleMap in a SupportMapFragment. In the FragmentActivity I initialized the map in the onCreate Method
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_frame);
// Initialize Google Maps Android API
try {
MapsInitializer.initialize(this);
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
...
the Code of my MapFragment looks exactly like this
public class MapFragment extends SupportMapFragment {
private final String TAG = getClass().getSimpleName();
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.map, null);
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
GoogleMap gMap = this.getMap();
Log.d(TAG,
"getMap() " + ((gMap == null) ? "null" : gMap.toString()));
// Setting Location and Zoom
gMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(
40.76793169992044f, -73.98180484771729f), 14.0f));
}
}
Every time I am getting null with the getMap Method... any ideas?
€dit:
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"
map:cameraBearing="112.5"
map:cameraTargetLat="-33.796923"
map:cameraTargetLng="150.922433"
map:cameraTilt="30"
map:cameraZoom="50"
map:mapType="normal"
map:uiCompass="false"
map:uiRotateGestures="true"
map:uiScrollGestures="false"
map:uiTiltGestures="true"
map:uiZoomControls="false"
map:uiZoomGestures="true" />

faced with similiar issue, after updating google play services to the latest version.
my problem was resolved by adding metadata in manifest
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
https://developers.google.com/maps/documentation/android/start#add_a_map

Related

GoogleMaps in FragmentActivity AndroidStudio

I'm developing an app using Android sdk version 29. I want to add Google Maps in one of my Fragments Activity but its not working and i couldnt fix it.
fradment_map.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.jHumildes.beautyappointment.Fragments.MapFragment">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/mapAPI"
android:name="com.google.android.gms.maps.SupportMapFragment"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
MapFragment.java
public class MapFragment extends Fragment implements OnMapReadyCallback {
GoogleMap mapAPI;
SupportMapFragment mapFragment;
public MapFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_map, container, false);
return view;
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager()
.findFragmentById(R.id.mapAPI);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mapAPI = googleMap;
LatLng Dublin = new LatLng(53.3581716, -6.2595678);
mapAPI.addMarker(new MarkerOptions().position(Dublin).title("Dublin"));
mapAPI.moveCamera(CameraUpdateFactory.newLatLng(Dublin));
}
}
I have implemented the libraries and also added uses-permissions in manifest.
implementation 'com.google.android.gms:play-services-maps:11.0.4'
implementation 'com.google.maps.android:android-maps-utils:0.4+'
...
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="AIzaSyDI-Hx6qi9dEkuGZL_c2sMMg-Cm2wd7wNg" />
</application>
map fragment
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
tools:layout_editor_absoluteX="58dp"
tools:layout_editor_absoluteY="257dp" />
map class:
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.onCreate(savedInstanceState);
mapFragment.onResume();
try {
MapsInitializer.initialize(getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}
mapFragment.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(GoogleMap mMap) {
googleMap = mMap;
googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
public void onMapClick(LatLng point) {
// Drawing marker on the map
}
});
googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker marker) {
// TODO Auto-generated method stub
}
});
googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(final Marker marker) {
DetailData(String.valueOf(marker.getTag()));
tag = String.valueOf(marker.getTag());
name = marker.getTitle();
return false;
}
});
}
});

MapView is not shown correctly

I am using MapView inside a RecycleView in android.
here is my xml code:
<com.google.android.gms.maps.MapView
android:id="#+id/map_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
and:
private fun initializeMap() {
val mapView : MapView = binding.root.findViewById(R.id.map_view)
mapView?.let {
mapView.onCreate(null)
mapView.getMapAsync(this)
}
}
override fun onMapReady(map: GoogleMap?) {
MapsInitializer.initialize(fragment.context)
googleMap = map
googleMap?.let {
it.setOnMapLoadedCallback { this }
it.setOnMapClickListener { this }
it.uiSettings.setAllGesturesEnabled(false)
it.uiSettings.isZoomControlsEnabled = false
}
}
override fun onMapLoaded() {
}
override fun onMapClick(p0: LatLng?) {
}
the map that appear is like following
and it takes a lot of time tp appear and also does not appear direct, I scroll a lot
also the methods onMapLoaded() and onMapClick() does not call
As you set it.uiSettings.setAllGesturesEnabled(false) and it.uiSettings.isZoomControlsEnabled = false use MapView in Lite Mode:
<com.google.android.gms.maps.MapView
android:id="#+id/map_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
map:cameraZoom="<ZOOM>"
map:mapType="normal"
map:liteMode="true">
Here you can find Official example of using MapView inside RecyclerView and here MapView layout settings:
<!-- MapView in lite mode. Note that it needs to be initialised
programmatically before it can be used. -->
<com.google.android.gms.maps.MapView
android:id="#+id/lite_listrow_map"
android:layout_width="match_parent"
android:layout_height="150dp"
map:liteMode="true" map:mapType="none" />
this my map.xml file
<com.google.android.gms.maps.MapView android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android" />
And this is java class
public class map extends Fragment implements OnMapReadyCallback {
GoogleMap mMap;
private MapView mapView;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.map,null);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng ahmedabad = new LatLng(23.022505, 72.571365);
BitmapDescriptor icon = BitmapDescriptorFactory.fromResource(R.drawable.icons8fiat50060);
MarkerOptions markerOptions = new MarkerOptions().position(ahmedabad)
.title("Current Location")
.snippet("Thinking of finding some thing...")
.icon(icon);
googleMap.addMarker(markerOptions);
mMap.getUiSettings().setZoomControlsEnabled(true);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(ahmedabad,30f));
}
}
this is quite easy i think this will helpful for you.

Setting up GoogleMap in Android app

I'm trying to add a googlemap into my app, but I couldn't get it to work.
I'm working with ViewPager on xamarin btw.
Layout:
...
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.google.android.gms.maps.MapFragment" />
...
My fragment:
public class InfoFragment : FragmentV4, IOnMapReadyCallback
{
private GoogleMap mMap;
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var view = inflater.Inflate(Resource.Layout.InfoFragment, container, false);
//Set up google maps coords in infos section
SetUpMap();
return view;
}
private void SetUpMap()
{
if (mMap == null) {
//((MapFragment)FragmentManager.FindFragmentById (Resource.Id.map)).GetMapAsync (this);
var mapFragment = (SupportMapFragment)Activity.SupportFragmentManager.FindFragmentById(Resource.Id.map);
mapFragment.GetMapAsync(this);
}
}
//Launching of the actual map
public void OnMapReady (GoogleMap googleMap)
{
//Setup cords and stuff...
}
}
During the setup of my map. When i do
var mapFragment = (SupportMapFragment)Activity.SupportFragmentManager.FindFragmentById(Resource.Id.map);
it always returns null. can someone help?
found the problem
android:name="com.google.android.gms.maps.MapFragment" />
should be:
android:name="com.google.android.gms.maps.SupportMapFragment" />
and in my fragment instead of:
var mapFragment = (SupportMapFragment)Activity.SupportFragmentManager.FindFragmentById(Resource.Id.map);
mapFragment.GetMapAsync(this);
it should be:
((SupportMapFragment)ChildFragmentManager.FindFragmentById(Resource.Id.map)).GetMapAsync(this);

Modifying Google Maps WaterMark

I have a Google Maps mapView in a ScrollView. it works fine, except when Scrolling I get some ugly black backgrounds along the bottom and top.
Here's a video of the issue:
https://www.youtube.com/watch?v=wLla7nS6Ehc&feature=youtu.be
To fix this, I wrapped the Map in a FrameLayout and made it bigger than the actual container, this fixes the issue, except now I can't see the Google watermark anymore. I tried using a bottom padding, but with the padding the black pattern shows up again, but this time only at the bottom.
Is it okay to put a TextView in the FrameLayout with the font and style of the Google Maps watermark as a work around? Am I violating the TOS if I do this?
This is the xml for my fix:
<FrameLayout
android:id="#+id/map_view_container"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_below="#+id/location"
android:layout_centerHorizontal="true"
android:layout_marginTop="#dimen/full_margin"
android:background="#color/transparent">
<com.google.android.gms.maps.MapView
android:id="#+id/map_view"
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_gravity="center"
android:background="#color/off_white"
map:mapType="normal" />
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/transparent" />
</FrameLayout>
EDIT: Fragment code:
public class MapFragment extends Fragment
implements View.OnClickListener,
com.google.android.gms.maps.OnMapReadyCallback {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_business_home, container, false);
final Bundle mapViewSavedInstanceState = savedInstanceState != null ? savedInstanceState.getBundle("mapViewSaveState") : null;
initializeViewComponents(rootView, mapViewSavedInstanceState);
return rootView;
}
private void initializeViewComponents(View rootView, Bundle savedInstanceState) {
scrollView = (ScrollView) rootView.findViewById(R.id.scroll_view);
mapView = (MapView) rootView.findViewById(R.id.map_view);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
MapsInitializer.initialize(this.getActivity());
this.map = googleMap;
// Gets to GoogleMap from the MapView and does initialization stuff
map.getUiSettings().setMyLocationButtonEnabled(true);
map.getUiSettings().setAllGesturesEnabled(false);
map.setMyLocationEnabled(true);
onAsyncLoad();
}
#Override
public void onResume() {
if (mapView != null) {
mapView.onResume();
}
super.onResume();
}
#Override
public void onLowMemory() {
if (mapView != null) {
mapView.onLowMemory();
}
super.onLowMemory();
}
public void onSaveInstanceState(Bundle outState) {
Log.i(TAG, "SAVING TO INSTANCE STATE");
//This MUST be done before saving any base class variables
final Bundle mapViewSaveState = new Bundle(outState);
if (mapView != null) { // May be null after onDestroView
mapView.onSaveInstanceState(mapViewSaveState);
outState.putBundle("mapViewSaveState", mapViewSaveState);
}
}

How to Add Google Map into fragment onActivityCreated

I would like to put an Google Map in a fragment, but please be noted that I want to put it inside the onActivityCreated, and the fragment class just need to extend fragment class only.
And my final layout will be:
This is a fragment contains a text, map and button.
~~~~~~~~~~~~~~~
Textview.....
Map.....
Button....
~~~~~~~~~~~~~~~
Also, the map need include my current location.
May I know how to do that? with java and xml please :)
In your Fragment layout put a MapView widget:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- My TextView -->
<com.google.android.gms.maps.MapView android:id="#+id/my_map"
android:layout_width="match_parent"
android:layout_height="match_parent"
map:uiZoomControls="false"
map:uiCompass="false"
map:uiRotateGestures="false"
map:uiScrollGestures="true"
map:uiTiltGestures="false"
map:uiZoomGestures="true"
android:visibility="visible"/>
<!-- My Button and eventually others widgets -->
</RelativeLayout>
Your fragment will inflate your custom layout with the MapView and the others widgets you need in . Here a pseudo code about how to setup the MapView.
public class MyCustomFragmentWithMap extends Fragment {
private MapView mMapView = null;
private GoogleMap mMyMap = null;
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
try {
MapsInitializer.initialize(getActivity());
} catch (GooglePlayServicesNotAvailableException err) {
Log.e(TAG, err.getMessage(), err);
}
mMapView = (MapView)getView().findViewById(R.id.my_map);
mMapView.onCreate(savedInstanceState);
mMyMap = mMapView.getMap();
mMyMap.setMyLocationEnabled(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.my_custom_fragment_with_map, container, false);
/* All the widgets setup as usual ... */
return view;
}
#Override
public void onResume() {
super.onResume();
if (mMapView != null)
mMapView.onResume();
}
#Override
public void onPause() {
if (mMapView != null)
mMapView.onPause();
super.onPause();
}
#Override
public void onDestroy() {
if (mMapView != null)
mMapView.onDestroy();
super.onDestroy();
}
#Override
public void onLowMemory() {
super.onLowMemory();
if (mMapView != null)
mMapView.onLowMemory();
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (mMapView != null)
mMapView.onSaveInstanceState(outState);
}
I'm assuming that you already know how to setup the Google Maps Android API v2 (obtaining the api key, setting permissions in manifest and all the boring stuffs :-P).

Categories

Resources