0
votes

I am planning to integrate Phunware map within Xamarin Forms app. I am able to create Android Binding project so I can get packages equivalent to Phunware Android native SDK. By this I am able to access all methods and classes of Phunware SDK. Now the challenge is I have to follow Phunware Android native sample app source code, write equivalent code in Xamarin Forms and bring the map. I have given Android UI xml and activity file for sample.

 <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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.phunware.java.sample.LoadBuildingActivity">

        <fragment xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/map"
            android:name="com.phunware.mapping.MapFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignTop="@id/map"
            android:background="@color/white"
            android:orientation="vertical"
            android:padding="6dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/switch_floors" />

            <Spinner
                android:id="@+id/floorSpinner"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </LinearLayout>

    </RelativeLayout>

    package com.landt.ismartphunware2;

    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.util.Log;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.Spinner;

    import com.google.android.gms.maps.CameraUpdate;
    import com.google.android.gms.maps.CameraUpdateFactory;
    import com.phunware.core.PwCoreSession;
    //import com.phunware.java.sample.R;
    import com.phunware.mapping.MapFragment;
    import com.phunware.mapping.OnPhunwareMapReadyCallback;
    import com.phunware.mapping.PhunwareMap;
    import com.phunware.mapping.manager.Callback;
    import com.phunware.mapping.manager.PhunwareMapManager;
    import com.phunware.mapping.model.Building;
    import com.phunware.mapping.model.FloorOptions;

    public class LoadBuildingActivity extends AppCompatActivity implements OnPhunwareMapReadyCallback {
        private static final String TAG = LoadBuildingActivity.class.getSimpleName();
        private PhunwareMapManager mapManager;
        private Building currentBuilding;
        private ArrayAdapter<FloorOptions> spinnerAdapter;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.load_building);

            Spinner floorSpinner = findViewById(R.id.floorSpinner);
            spinnerAdapter = new FloorAdapter(this);
            floorSpinner.setAdapter(spinnerAdapter);
            floorSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    FloorOptions floor = spinnerAdapter.getItem((int) id);
                    if (currentBuilding != null && floor != null) {
                        currentBuilding.selectFloor(floor.getLevel());
                    }
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {
                }
            });

            // Create the map manager used to load the building
            mapManager = PhunwareMapManager.create(this);

            // Register the Phunware API keys
            PwCoreSession.getInstance().registerKeys(this);

            MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
            if (mapFragment != null) {
                mapFragment.getPhunwareMapAsync(this);
            }
        }

        @Override
        public void onPhunwareMapReady(final PhunwareMap phunwareMap) {
            // Retrieve buildingId from integers.xml
            int buildingId = getResources().getInteger(R.integer.buildingId);

            mapManager.setPhunwareMap(phunwareMap);
            mapManager.addBuilding(buildingId,
                    new Callback<Building>() {
                        @Override
                        public void onSuccess(Building building) {
                            Log.d(TAG, "Building loaded successfully");
                            currentBuilding = building;

                            // Populate floor spinner
                            spinnerAdapter.clear();
                            spinnerAdapter.addAll(building.getBuildingOptions().getFloors());

                            // Set building to initial floor value
                            FloorOptions initialFloor = building.initialFloor();
                            building.selectFloor(initialFloor.getLevel());

                            // Animate the camera to the building at an appropriate zoom level
                            CameraUpdate cameraUpdate = CameraUpdateFactory
                                    .newLatLngBounds(initialFloor.getBounds(), 4);
                            phunwareMap.getGoogleMap().animateCamera(cameraUpdate);
                        }

                        @Override
                        public void onFailure(Throwable throwable) {
                            Log.d(TAG, "Error when loading building -- " + throwable.getMessage());
                        }
                    });
        }
    }

Here, they are casting UI control to MapFragment and calling getPhunwareMapAsync() method. I just want to know in Xamarin Forms what control we can map to this MapFragment? In case we are about to write equivalent code in MainActivity of Xamarin Android project, how can we establish connection between PCL project and Android project and bring the map in View? I am just trying to figure out how to start and what to put where as I have excellent experience in Xamarin Forms but less experience with Android Native.

1

1 Answers

2
votes

Suggetion 1:

You could try using Xamarin.Forms.Maps:

Xamarin.Forms.Maps provides a cross-platform abstraction for displaying maps that use the native map APIs on each platform, to provide a fast and familiar map experience for users.

  • Xamarin.Forms Map
    • Basic usage about Xamarin.Forms.Map
    • Map functionality can be further enhanced by creating a map custom renderer.
  • Customizing a Xamarin.Forms Map
    • This article explains how to create a custom renderer for the Map control, which displays a native map with a customized pin and a customized view of the pin data on each platform.

Suggestion 2:

Customizing a ContentPage to display the Android native page that had a map along with some other controls:

A ContentPage is a visual element that displays a single view and occupies most of the screen. This article demonstrates how to create a custom renderer for the ContentPage page, enabling developers to override the default native rendering with their own platform-specific customization.