Unable to add xamarin.forms.map control to Xamarin Form

Issue

I want to add a map control to my android app. To do this I have got the latest xamarin.forms and xamarin.forms.maps packages from Nuget.

My code behind logic does nothing at all, and my axml page looks like as below:

<?xml version"1.0" encoding"utf-8"?>
<LinearLayout
    xmlns:android"http://schemas.android.com/apk/res/android"
    xmlns:maps"clr-namespace:Xamarin.Forms.Maps;assemblyXamarin.Forms.Maps"
    xmlns:x"http://schemas.microsoft.com/winfx/2009/xaml"
    android:orientation"vertical"
    android:layout_width"match_parent"
    android:layout_height"match_parent"
    xmlns:d"http://xamarin.com/schemas/2014/forms/design"
    xmlns:mc"http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable"d">
        <LinearLayout
            android:orientation"vertical"
            android:minWidth"25px"
            android:minHeight"25px"
            android:layout_width"match_parent"
            android:layout_height"match_parent"
            android:id"@+id/optionsContainerLinearLayout">
           //Other UI elements
        <maps:Map>
        </maps:Map>
        </LinearLayout>
...
</LinearLayout>

Note I’ve but no tags within maps:Map so as to provide minimal chance for errors. Note that when I populate this map with more properties as directly below, the code still breaks in the same way:

<map:Map
        android:minWidth"25px"
        android:minHeight"25px"
        android:layout_width"match_parent"
        android:layout_height"wrap_content"
        android:id"@+id/mockMapView">
</map:Map>

When I run this application, it breaks on SetContentView() with the error:

Didn't find class "android.view.Map" on path: DexPathList
[[zip file "/system/framework/org.apache.http.legacy.boot.jar", zip file "/data/app/com.geosolve.pavestate2-fJtHBLcT2gY6AlUrxcOOVg/base.apk"],
nativeLibraryDirectories[/data/app/com.geosolve.pavestate2-fJtHBLcT2gY6AlUrxcOOVg/lib/x86, /data/app/com.geosolve.pavestate2-fJtHBLcT2gY6AlUrxcOOVg/base.apk!/lib/x86, /system/lib]]

I’ve got another sample xamarin application I downloaded which runs fine on my emulator, so I don’t think the emulator is the problem. I have my API key being used by this other application as well, so I don’t think the API key is the problem either.

My androidManifest.xml looks like so:

...
    <application android:allowBackup"true" android:icon"@mipmap/icon" android:label"@string/app_name" android:roundIcon"@mipmap/icon" android:supportsRtl"true" android:theme"@style/GeosolveApp">
        <meta-data android:name"com.google.android.maps.v2.API_KEY" android:value"(My API Key)" />
    <meta-data
    android:name"com.google.android.gms.version"
    android:value"@integer/google_play_services_version" />
    </application>
...

Exception

Any help as to why this isn;’t just showing up like you would expect?

Solution

For Xamarin.Android,we usually install the Xamarin.GooglePlayServices.Maps package from NuGet.

And add the map in your xaml like:

Declaratively :

<fragment 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"
   android:id"@+id/map"
   class"com.google.android.gms.maps.SupportMapFragment" /> 

Programmatically :

var mapFrag  MapFragment.NewInstance();
activity.FragmentManager.BeginTransaction()
                        .Add(Resource.Id.map_container, mapFrag, "map_fragment")
                        .Commit();

the more you could read Google Maps.

Answered By – Leo Zhu – MSFT

Leave a Comment