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);
Related
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;
}
});
}
});
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.
I want to add map marker in my map. I have a fragment in which I want to show map marker. I have added 'com.google.android.gms:play-services-maps:10.0.1' in my app's gradle. But I an having error i.e.
android.view.InflateException: Binary XML file line #7: Binary XML file line #7: Error inflating class fragment
// xml file code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="#dimen/dp176"
android:name="com.google.android.gms.maps.SupportMapFragment" />
</LinearLayout>
// code for map
public class KantorenOfficeDetailsFragment extends Fragment implements View.OnClickListener, OnMapReadyCallback {
GoogleMap googleMap;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_kantoren_office_details, container, false);
setUpMapIfNeeded();
return v;
}
private void setUpMapIfNeeded() {
if (googleMap == null) {
SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
if (googleMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
googleMap.addMarker(new MarkerOptions().position(new LatLng(22.7253, 75.8655)).title("Indore")); // here is marker Adding code
}
}
you are not initialize you googleMap object.
MapFragment Initialize must like this
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
mapFragment= (MyMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
else
mapFragment= (MyMapFragment) getFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
and then import the callback method
#Override
public void onMapReady(GoogleMap map) {
googleMap = map;
}
call this setUpMap in inside callback method
if (googleMap != null) {
setUpMap();
}
Your OnMapReady Must be like
#Override
public void onMapReady(GoogleMap map) {
googleMap = map;
if (googleMap != null) {
setUpMap();
}
}
Hope it will help you
I'm trying to initialize a GoogleMap instance, but SupportMapFragment returns null.
Here is my MainActivity Class:
public class MainActivity extends FragmentActivity
{
GoogleMap gMap;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
initMap(); //here is the NullPointerExeption
setContentView(R.layout.activity_map);
}
public boolean initMap()
{
if (gMap == null) {
SupportMapFragment mapFrag =
(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.fragMap);
gMap = mapFrag.getMap();
}
return (gMap != null);
}
}
And this is my activity_map.xml:
<?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/fragMap"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
what is the problem? if I comment out the initMap() function, it works fine and displays the map.
I think you should change the order like
setContentView(R.layout.activity_map);
initMap();
First you should setContentView(R.layout.activity_map); and then reference Map from SupportMapFragment.
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