AndroidX Fragment add Google Map












3















I am trying to add a Google map in AndroidX Fragment(androidx.fragment.app.Fragment) using MVP pattern but it's not showing up. But when I tried to use this code in normal Fragment(android.support.v4.app.Fragment) or Activity it's showing up fine.



I have tried both ways:




  1. using MapView (Which is commented in code)


  2. using SupportMapFragment (Which not supports by AndroidX ref here)



Here is the code what I have tried:



import android.os.Bundle
import com.google.android.gms.maps.*
import com.google.android.gms.maps.model.LatLng
import com.test.App
import com.test.R
import com.test.ui.base.BaseFragment
import javax.inject.Inject
import com.google.android.gms.maps.model.MarkerOptions
import com.google.android.gms.maps.SupportMapFragment

class OverviewMapFragment : BaseFragment<OverviewMapContract.View, OverviewMapContract.Presenter>(), OverviewMapContract.View, OnMapReadyCallback {
private var map: GoogleMap? = null
// private var mapView: MapView? = null

override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)

val mapFragment = childFragmentManager.findFragmentById(R.id.map) as SupportMapFragment
mapFragment.getMapAsync(this)

// mapView = view!!.findViewById(R.id.map_view) as MapView
// mapView!!.onCreate(savedInstanceState);
// mapView!!.getMapAsync(this)
}

/*override fun onStart() {
super.onStart()
mapView!!.onStart()
}

override fun onResume() {
super.onResume()
mapView!!.onResume()
}

override fun onDestroy() {
mapView!!.onDestroy()
super.onDestroy()
}

override fun onLowMemory() {
super.onLowMemory()
mapView!!.onLowMemory()
}

override fun onStop() {
super.onStop()
mapView!!.onStop()
}

override fun onPause() {
mapView!!.onPause()
super.onPause()
}

override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
mapView!!.onSaveInstanceState(outState)
}*/

override fun onMapReady(googleMap: GoogleMap?) {
map = googleMap
map!!.setMinZoomPreference(14.0f)
val ny = LatLng(40.7143528, -74.0059731)
map!!.addMarker(MarkerOptions().position(ny));
map!!.moveCamera(CameraUpdateFactory.newLatLng(ny))
}


xml:



<?xml version="1.0" encoding="utf-8"?>

<LinearLayout 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">

<!--<com.google.android.gms.maps.MapView
android:id="@+id/map_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />-->

<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.test.ui.components.overviewmap.OverviewMapFragment" />

</LinearLayout>


Manifest:



<uses-permission android:name="android.permission.INTERNET" />

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

<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>

<permission
android:name="com.example.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.example.permission.MAPS_RECEIVE"/>

<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="@string/google_maps_key" />

<uses-library android:name="com.google.android.maps" />


Used googleMapVersion 16.0.0:



implementation 'com.google.android.gms:play-services-maps:$googleMapVersion' 









share|improve this question




















  • 1





    Possible duplicate of SupportMapFragment does not support AndroidX Fragment

    – xomena
    Nov 21 '18 at 23:09
















3















I am trying to add a Google map in AndroidX Fragment(androidx.fragment.app.Fragment) using MVP pattern but it's not showing up. But when I tried to use this code in normal Fragment(android.support.v4.app.Fragment) or Activity it's showing up fine.



I have tried both ways:




  1. using MapView (Which is commented in code)


  2. using SupportMapFragment (Which not supports by AndroidX ref here)



Here is the code what I have tried:



import android.os.Bundle
import com.google.android.gms.maps.*
import com.google.android.gms.maps.model.LatLng
import com.test.App
import com.test.R
import com.test.ui.base.BaseFragment
import javax.inject.Inject
import com.google.android.gms.maps.model.MarkerOptions
import com.google.android.gms.maps.SupportMapFragment

class OverviewMapFragment : BaseFragment<OverviewMapContract.View, OverviewMapContract.Presenter>(), OverviewMapContract.View, OnMapReadyCallback {
private var map: GoogleMap? = null
// private var mapView: MapView? = null

override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)

val mapFragment = childFragmentManager.findFragmentById(R.id.map) as SupportMapFragment
mapFragment.getMapAsync(this)

// mapView = view!!.findViewById(R.id.map_view) as MapView
// mapView!!.onCreate(savedInstanceState);
// mapView!!.getMapAsync(this)
}

/*override fun onStart() {
super.onStart()
mapView!!.onStart()
}

override fun onResume() {
super.onResume()
mapView!!.onResume()
}

override fun onDestroy() {
mapView!!.onDestroy()
super.onDestroy()
}

override fun onLowMemory() {
super.onLowMemory()
mapView!!.onLowMemory()
}

override fun onStop() {
super.onStop()
mapView!!.onStop()
}

override fun onPause() {
mapView!!.onPause()
super.onPause()
}

override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
mapView!!.onSaveInstanceState(outState)
}*/

override fun onMapReady(googleMap: GoogleMap?) {
map = googleMap
map!!.setMinZoomPreference(14.0f)
val ny = LatLng(40.7143528, -74.0059731)
map!!.addMarker(MarkerOptions().position(ny));
map!!.moveCamera(CameraUpdateFactory.newLatLng(ny))
}


xml:



<?xml version="1.0" encoding="utf-8"?>

<LinearLayout 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">

<!--<com.google.android.gms.maps.MapView
android:id="@+id/map_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />-->

<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.test.ui.components.overviewmap.OverviewMapFragment" />

</LinearLayout>


Manifest:



<uses-permission android:name="android.permission.INTERNET" />

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

<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>

<permission
android:name="com.example.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.example.permission.MAPS_RECEIVE"/>

<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="@string/google_maps_key" />

<uses-library android:name="com.google.android.maps" />


Used googleMapVersion 16.0.0:



implementation 'com.google.android.gms:play-services-maps:$googleMapVersion' 









share|improve this question




















  • 1





    Possible duplicate of SupportMapFragment does not support AndroidX Fragment

    – xomena
    Nov 21 '18 at 23:09














3












3








3


2






I am trying to add a Google map in AndroidX Fragment(androidx.fragment.app.Fragment) using MVP pattern but it's not showing up. But when I tried to use this code in normal Fragment(android.support.v4.app.Fragment) or Activity it's showing up fine.



I have tried both ways:




  1. using MapView (Which is commented in code)


  2. using SupportMapFragment (Which not supports by AndroidX ref here)



Here is the code what I have tried:



import android.os.Bundle
import com.google.android.gms.maps.*
import com.google.android.gms.maps.model.LatLng
import com.test.App
import com.test.R
import com.test.ui.base.BaseFragment
import javax.inject.Inject
import com.google.android.gms.maps.model.MarkerOptions
import com.google.android.gms.maps.SupportMapFragment

class OverviewMapFragment : BaseFragment<OverviewMapContract.View, OverviewMapContract.Presenter>(), OverviewMapContract.View, OnMapReadyCallback {
private var map: GoogleMap? = null
// private var mapView: MapView? = null

override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)

val mapFragment = childFragmentManager.findFragmentById(R.id.map) as SupportMapFragment
mapFragment.getMapAsync(this)

// mapView = view!!.findViewById(R.id.map_view) as MapView
// mapView!!.onCreate(savedInstanceState);
// mapView!!.getMapAsync(this)
}

/*override fun onStart() {
super.onStart()
mapView!!.onStart()
}

override fun onResume() {
super.onResume()
mapView!!.onResume()
}

override fun onDestroy() {
mapView!!.onDestroy()
super.onDestroy()
}

override fun onLowMemory() {
super.onLowMemory()
mapView!!.onLowMemory()
}

override fun onStop() {
super.onStop()
mapView!!.onStop()
}

override fun onPause() {
mapView!!.onPause()
super.onPause()
}

override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
mapView!!.onSaveInstanceState(outState)
}*/

override fun onMapReady(googleMap: GoogleMap?) {
map = googleMap
map!!.setMinZoomPreference(14.0f)
val ny = LatLng(40.7143528, -74.0059731)
map!!.addMarker(MarkerOptions().position(ny));
map!!.moveCamera(CameraUpdateFactory.newLatLng(ny))
}


xml:



<?xml version="1.0" encoding="utf-8"?>

<LinearLayout 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">

<!--<com.google.android.gms.maps.MapView
android:id="@+id/map_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />-->

<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.test.ui.components.overviewmap.OverviewMapFragment" />

</LinearLayout>


Manifest:



<uses-permission android:name="android.permission.INTERNET" />

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

<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>

<permission
android:name="com.example.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.example.permission.MAPS_RECEIVE"/>

<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="@string/google_maps_key" />

<uses-library android:name="com.google.android.maps" />


Used googleMapVersion 16.0.0:



implementation 'com.google.android.gms:play-services-maps:$googleMapVersion' 









share|improve this question
















I am trying to add a Google map in AndroidX Fragment(androidx.fragment.app.Fragment) using MVP pattern but it's not showing up. But when I tried to use this code in normal Fragment(android.support.v4.app.Fragment) or Activity it's showing up fine.



I have tried both ways:




  1. using MapView (Which is commented in code)


  2. using SupportMapFragment (Which not supports by AndroidX ref here)



Here is the code what I have tried:



import android.os.Bundle
import com.google.android.gms.maps.*
import com.google.android.gms.maps.model.LatLng
import com.test.App
import com.test.R
import com.test.ui.base.BaseFragment
import javax.inject.Inject
import com.google.android.gms.maps.model.MarkerOptions
import com.google.android.gms.maps.SupportMapFragment

class OverviewMapFragment : BaseFragment<OverviewMapContract.View, OverviewMapContract.Presenter>(), OverviewMapContract.View, OnMapReadyCallback {
private var map: GoogleMap? = null
// private var mapView: MapView? = null

override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)

val mapFragment = childFragmentManager.findFragmentById(R.id.map) as SupportMapFragment
mapFragment.getMapAsync(this)

// mapView = view!!.findViewById(R.id.map_view) as MapView
// mapView!!.onCreate(savedInstanceState);
// mapView!!.getMapAsync(this)
}

/*override fun onStart() {
super.onStart()
mapView!!.onStart()
}

override fun onResume() {
super.onResume()
mapView!!.onResume()
}

override fun onDestroy() {
mapView!!.onDestroy()
super.onDestroy()
}

override fun onLowMemory() {
super.onLowMemory()
mapView!!.onLowMemory()
}

override fun onStop() {
super.onStop()
mapView!!.onStop()
}

override fun onPause() {
mapView!!.onPause()
super.onPause()
}

override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
mapView!!.onSaveInstanceState(outState)
}*/

override fun onMapReady(googleMap: GoogleMap?) {
map = googleMap
map!!.setMinZoomPreference(14.0f)
val ny = LatLng(40.7143528, -74.0059731)
map!!.addMarker(MarkerOptions().position(ny));
map!!.moveCamera(CameraUpdateFactory.newLatLng(ny))
}


xml:



<?xml version="1.0" encoding="utf-8"?>

<LinearLayout 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">

<!--<com.google.android.gms.maps.MapView
android:id="@+id/map_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />-->

<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.test.ui.components.overviewmap.OverviewMapFragment" />

</LinearLayout>


Manifest:



<uses-permission android:name="android.permission.INTERNET" />

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

<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>

<permission
android:name="com.example.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.example.permission.MAPS_RECEIVE"/>

<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="@string/google_maps_key" />

<uses-library android:name="com.google.android.maps" />


Used googleMapVersion 16.0.0:



implementation 'com.google.android.gms:play-services-maps:$googleMapVersion' 






android android-fragments google-maps-android-api-2 androidx






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 4:33







Shylendra Madda

















asked Nov 21 '18 at 7:20









Shylendra MaddaShylendra Madda

11.2k64781




11.2k64781








  • 1





    Possible duplicate of SupportMapFragment does not support AndroidX Fragment

    – xomena
    Nov 21 '18 at 23:09














  • 1





    Possible duplicate of SupportMapFragment does not support AndroidX Fragment

    – xomena
    Nov 21 '18 at 23:09








1




1





Possible duplicate of SupportMapFragment does not support AndroidX Fragment

– xomena
Nov 21 '18 at 23:09





Possible duplicate of SupportMapFragment does not support AndroidX Fragment

– xomena
Nov 21 '18 at 23:09












1 Answer
1






active

oldest

votes


















0














Finally I found the solution by using MapView (Which is commented in code) and calling onMapReady by a little delay. Now map showing up fine with MapView.



override fun initUI() {
mapView?.postDelayed({
mapView?.onCreate(Bundle())
mapView?.getMapAsync {
onMapReady(it)
}
}, 500)
}

private fun onMapReady(googleMap: GoogleMap?) {
map = googleMap
// map?.setMinZoomPreference(14.0f)
val ny = LatLng(40.7143528, -74.0059731)
map?.addMarker(MarkerOptions().position(ny));
map?.moveCamera(CameraUpdateFactory.newLatLng(ny))
}





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53407044%2fandroidx-fragment-add-google-map%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Finally I found the solution by using MapView (Which is commented in code) and calling onMapReady by a little delay. Now map showing up fine with MapView.



    override fun initUI() {
    mapView?.postDelayed({
    mapView?.onCreate(Bundle())
    mapView?.getMapAsync {
    onMapReady(it)
    }
    }, 500)
    }

    private fun onMapReady(googleMap: GoogleMap?) {
    map = googleMap
    // map?.setMinZoomPreference(14.0f)
    val ny = LatLng(40.7143528, -74.0059731)
    map?.addMarker(MarkerOptions().position(ny));
    map?.moveCamera(CameraUpdateFactory.newLatLng(ny))
    }





    share|improve this answer






























      0














      Finally I found the solution by using MapView (Which is commented in code) and calling onMapReady by a little delay. Now map showing up fine with MapView.



      override fun initUI() {
      mapView?.postDelayed({
      mapView?.onCreate(Bundle())
      mapView?.getMapAsync {
      onMapReady(it)
      }
      }, 500)
      }

      private fun onMapReady(googleMap: GoogleMap?) {
      map = googleMap
      // map?.setMinZoomPreference(14.0f)
      val ny = LatLng(40.7143528, -74.0059731)
      map?.addMarker(MarkerOptions().position(ny));
      map?.moveCamera(CameraUpdateFactory.newLatLng(ny))
      }





      share|improve this answer




























        0












        0








        0







        Finally I found the solution by using MapView (Which is commented in code) and calling onMapReady by a little delay. Now map showing up fine with MapView.



        override fun initUI() {
        mapView?.postDelayed({
        mapView?.onCreate(Bundle())
        mapView?.getMapAsync {
        onMapReady(it)
        }
        }, 500)
        }

        private fun onMapReady(googleMap: GoogleMap?) {
        map = googleMap
        // map?.setMinZoomPreference(14.0f)
        val ny = LatLng(40.7143528, -74.0059731)
        map?.addMarker(MarkerOptions().position(ny));
        map?.moveCamera(CameraUpdateFactory.newLatLng(ny))
        }





        share|improve this answer















        Finally I found the solution by using MapView (Which is commented in code) and calling onMapReady by a little delay. Now map showing up fine with MapView.



        override fun initUI() {
        mapView?.postDelayed({
        mapView?.onCreate(Bundle())
        mapView?.getMapAsync {
        onMapReady(it)
        }
        }, 500)
        }

        private fun onMapReady(googleMap: GoogleMap?) {
        map = googleMap
        // map?.setMinZoomPreference(14.0f)
        val ny = LatLng(40.7143528, -74.0059731)
        map?.addMarker(MarkerOptions().position(ny));
        map?.moveCamera(CameraUpdateFactory.newLatLng(ny))
        }






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 23 '18 at 8:47

























        answered Nov 22 '18 at 4:43









        Shylendra MaddaShylendra Madda

        11.2k64781




        11.2k64781






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53407044%2fandroidx-fragment-add-google-map%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

            ts Property 'filter' does not exist on type '{}'

            mat-slide-toggle shouldn't change it's state when I click cancel in confirmation window