Issue
So the issue that I'm having is that i'm not sure how to access the map to set the initial location. The Fragment is always coming back NULL so it seems not to call the getMapReady function. I'm just not sure how to correctly reference the map widget in the layout so it will call the setUp for the map correctly.
THE LAYOUT
<FrameLayout 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.example.user.thisweekintown.CalendarFragment">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/mySwipe"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/bus_list_recycle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="horizontal" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
<fragment
android:id="@+id/businessslocation"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="15dp"
android:background="@color/cardview_light_background"/>
</FrameLayout>
The CODE
class BusListFragment : Fragment(),OnMapReadyCallback {
lateinit var mMap: GoogleMap
var mapView: SupportMapFragment? = null
var totaladd:String? = null
.........
AND THEN
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val view : View = inflater.inflate(R.layout.fragment_bus_list,container,false)
var social_recycle: RecyclerView = view.findViewById(R.id.bus_list_recycle)
Log.d("should be calling", "onMapReady")
swipey = view.findViewById(R.id.mySwipe)
swipey.setColorSchemeColors(Color.RED, Color.BLUE, Color.GREEN, Color.CYAN);
swipey.setOnRefreshListener(SwipeRefreshLayout.OnRefreshListener { setupRecycle(true) })
calFrPrefs = context?.getSharedPreferences("main_prefs",0)
(activity as MainActivity).getSupportActionBar()?.setTitle(calFrPrefs!!.getString("current_town_name","No Town")+ " Businesses")
setupRecycle(false)
return view
}
AND THEN
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
mapView = childFragmentManager.findFragmentById(R.id.businessslocation) as? SupportMapFragment
Log.d("isMapView", mapView.toString())
mapView?.getMapAsync(this)
}
It should call the following
override fun onMapReady(googleMap: GoogleMap){
mMap = googleMap
Log.d("onMapready", "MapReady")
val cphbusiness = LatLng(42.187, 71.3065)
mMap.addMarker(MarkerOptions().position(cphbusiness).title("Her"))
mMap.moveCamera(CameraUpdateFactory.zoomTo(16.0F))
mMap.moveCamera(CameraUpdateFactory.newLatLng(cphbusiness))
val geocoder = Geocoder(context)
if (geocoder is Geocoder) {
var list: List<Address> = geocoder.getFromLocationName(totaladd, 1)
if(list.size >0) {
var mAddress: Address = list.get(0)
Log.d("latitiude", mAddress.latitude.toString())
Log.d("latitiude", mAddress.longitude.toString())
var address1 = LatLng(mAddress.latitude, mAddress.longitude)
changeMap(address1)
}else
{
Toast.makeText(context, "Unable to find Valid Location...Centering on Random place", Toast.LENGTH_LONG).show()
changeMap(cphbusiness)
}
} else {
}
}
fun changeMap(myLL: LatLng) {
mMap.moveCamera(CameraUpdateFactory.newLatLng(myLL))
mMap.addMarker(MarkerOptions().position(myLL))
}
Solution
Finally I resolve this issue by changing fragments name from android:name="com.google.android.gms.maps.MapFragment"
to android:name="com.google.android.gms.maps.SupportMapFragment"
Answered By - Marat Zangiev
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.