Google Maps API v2 Prerequisites
Note:
COMPILETODALVIK : UNEXPECTED TOP-LEVEL error :
Xamarin support already given me the answer to this. Youhave to set your android project's property setting to: Java Max Heap Size =512M
major version 51 is newer than 50, thehighest major version supported by this compiler.
Afteruninstalling Java 6 JDK (C:\Program Files (x86)\Java\jdk1.6.0_36), installingJava 7 JDK and rebooting my system I don't get any warnings anymore. Btw. it'simportant to install Java 7 JDK 32bit version although you may working on a x64system.
Link:
Severalitems need to be configured before you can use the Maps API, including:
· Installing the Google Play Services SDK.
· Creating an emulator with the Google APIs.
· Obtaining a Maps API key.
Binding Google Play Services
· Once the Google Play Services clientlibrary is installed, it must be bound by a Xamarin.Android Java bindinglibrary.
Specify the Required Permissions
Thefollowing permissions must be specified in the AndroidManifest.XML for theGoogle Maps Android API v2:
Thefollowing snippet is an example of the settings that must be added toAndroidManifest.XML:
<?xmlversion="1.0" encoding="utf-8"?>
<manifestxmlns:android="http://schemas.android.com/apk/res/android"android:versionName="4.5" package="com.xamarin.docs.android.mapsandlocationdemo2"android:versionCode="6">
<uses-sdkandroid:minSdkVersion="14" android:targetSdkVersion="17"/>
<!-- Google Maps for Android v2 requiresOpenGL ES v2 -->
<uses-feature android:glEsVersion="0x00020000"android:required="true" />
<!-- We need to be able to download maptiles and access Google Play Services-->
<uses-permissionandroid:name="android.permission.INTERNET" />
<!-- Allow the application to accessGoogle web-based services. -->
<uses-permissionandroid:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<!-- Google Maps for Android v2 willcache map tiles on external storage -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<!-- Google Maps for Android v2 needsthis permission so that it may check the connection state as it must downloaddata -->
<uses-permissionandroid:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- Permission to receive remotenotifications from Google Play Services -->
<!-- Notice here that we have thepackage name of our application as a prefix on the permissions. -->
<uses-permissionandroid:name="<PACKAGE NAME>.permission.MAPS_RECEIVE" />
<permissionandroid:name="<PACKAGE NAME>.permission.MAPS_RECEIVE"android:protectionLevel="signature" />
<!-- These are optional, butrecommended. They will allow Maps to use the My Location provider. -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permissionandroid:name="android.permission.ACCESS_FINE_LOCATION" />
<applicationandroid:label="@string/app_name">
<!-- Put your Google Maps V2 API Keyhere. -->
<meta-data android:name="com.google.android.maps.v2.API_KEY"android:value="YOUR_API_KEY" />
<meta-dataandroid:name="com.google.android.gms.version"android:value="@integer/google_play_services_version" />
</application>
</manifest>
Obtaining a GoogleMaps API Key
Link: http://developer.xamarin.com/guides/android/platform_features/maps_and_location/maps/obtaining_a_google_maps_api_key/
Run keytool usingthe following command:
You will need the SHA1 fingerprintlater on.
keytool -list -v -keystore"/Users/[UserName]/AppData/Local/Xamarin/Mono forAndroid/debug.keystore" -alias androiddebugkey -storepass android -keypassandroid
Creating an APIproject
Once you have the SHA1 fingerprint of the signingkeystores, it is necessary to create a new project in the Google APIs console,or to add the Google Maps Android API v2 service to an existing project.
Click on the APIs & authand scroll down to the list of available API's until you GoogleMaps Android API V2 . Toggle the switch indicator at the right to turnthe service on for this project
Obtaining the APIKey
Once the Console API project has been created, it isnecessary to create an Android API key. Xamarin.Android applications requirethe API key before they will be granted access to Android Map API v2.
1.Click on the APIs & auth > Credentials link inthe left hand menu:
2.Click on theAndroid key button in the dialog that pops up:
3.next, it will benecessary to white-list an application with this API key. Enter the SHA1fingerprint, followed by a semi-colon, followed by the package name of your applicaiton.The following line is an example:
BB:0D:AC:74:D3:21:E1:43:67:71:9B:62:91:AF:A1:66:6E:44:5D:75;com.example.android.mapexample
This can be seenin the following screenshot:
4.Once you clickon the Create button, you will be returned to the APIs & auth page whichwill display the API key and the Android apps that are authorized to use theAPI key:
AddingA MapFragment To An Activity
Similar to other Fragment classes, there are two ways toadd the MapFragment to an Activity:
1.Declaratively - The MapFragment can be added via theXML layout file for the Activity. The following XML snippet shows an example ofhow to use the <fragment> element:
<?xmlversion="1.0" encoding="utf-8"?>
<fragment xmlns:android=http://schemas.android.com/apk/res/android
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment"/>
public class BusinessDetailsFrag : MvxFragment
{
SupportMapFragment mapFrag;
public overrideView OnCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState)
{
var ignored = base.OnCreateView(inflater, container, savedInstanceState);
return this.BindingInflate(Resource.Layout.BusinessDetailsView,null);
}
public overridevoid OnDestroyView()
{
base.OnDestroyView();
if (mapFrag != null)
{
FragmentManager.BeginTransaction().Remove(mapFrag).Commit();
}
}
public overridevoid OnActivityCreated(Bundle savedInstanceState)
{
mapFrag = (SupportMapFragment)FragmentManager.FindFragmentById(Resource.Id.businessMap);
GoogleMap map = mapFrag.Map;
if (map != null)
{
int lat = -28;
int lon = 50;
var latLng = new LatLng(lat, lon);
map.UiSettings.ZoomControlsEnabled = true;
map.UiSettings.CompassEnabled = true;
map.MapType = GoogleMap.MapTypeNormal;
MarkerOptionsmarkerOpt1 =new MarkerOptions();
markerOpt1.SetPosition(newLatLng(lat, lon));
map.AddMarker(markerOpt1);
map.AnimateCamera(CameraUpdateFactory.NewLatLngZoom(latLng,13));
}
}