An old code works perfectly in this way:
LatLng location = new LatLng (myClass.myLocation.Latitude, myClass.myLocation.Longitude);
CameraPosition.Builder builder = CameraPosition.InvokeBuilder ();
builder.Target (location);
builder.Zoom (18);
CameraPosition cameraPosition = builder.Build ();
MapsInitializer.Initialize (this);
CameraUpdate cameraUpdate = CameraUpdateFactory.NewCameraPosition (cameraPosition);
MapFragment googleMap = FragmentManager.FindFragmentById<MapFragment> (Resource.Id.map);
theMap = googleMap.Map;
if (theMap != null) {
theMap.MapType = GoogleMap.MapTypeNormal;
theMap.MoveCamera (cameraUpdate);
}
but now that the .Map
is obsolete and deprecated, I must to use .GetMapAsync
in some way:
theMap = googleMap.GetMapAsync (IOnMapReadyCallback);
But I don't understand how.
There is somebody that can help me?
Your map fragment class must implement OnMapReadyCallback
and override onMapReady()
:
@Override
public void onMapReady(final GoogleMap map) {
this.map = map;
map.setMyLocationEnabled(true);
}
In your onCreateView
use getMapAsync()
to set the callback on the fragment:
MapFragment mapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map); mapFragment.getMapAsync(this);
All that you need to implement the google maps V2 is here: https://developers.google.com/maps/documentation/android/map
This might be late reply to your question, but may helpful to somebody else with the same issue. GetMapAsync() expects implementation of callback object of type IOnMapReadyCallback.
Find more detail here in my blog entry: http://appliedcodelog.com/2015/08/androidgmsmapsmapfragmentmap-is.html
//OnMapReadyClass
public class OnMapReadyClass :Java.Lang.Object,IOnMapReadyCallback
{
public GoogleMap Map { get; private set; }
public event Action<GoogleMap> MapReadyAction;
public void OnMapReady (GoogleMap googleMap)
{
Map = googleMap;
if ( MapReadyAction != null )
MapReadyAction (Map);
}
}
Now Call the GetMapAsync() and event Action return map instance on successful Map initialization.
//MyActivityClass.cs
GoogleMap map;
bool SetUpGoogleMap()
{
if(null != map ) return false;
var frag = FragmentManager.FindFragmentById<mapfragment>(Resource.Id.map);
var mapReadyCallback = new OnMapReadyClass();
mapReadyCallback.MapReadyAction += delegate(GoogleMap googleMap )
{
map=googleMap;
};
frag.GetMapAsync(mapReadyCallback);
return true;
}
Please see this:
public class MapActivity : Activity, IOnMapReadyCallback
{
public void OnMapReady(GoogleMap googleMap)
{
if (googleMap != null)
{
//Map is ready for use
}
}
}
Hope this will help you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With