Introduction
Let's build a simple Map application that will contain the source and destination addresses in the form of fields From and To respectively.
This application will take two user inputs for the map, i.e., Latitude and Longitude. Clicking on the open in the map will open the map that can show you the direction from the current location to the given address defined by the two user inputs.
This application will show you how to open the map without giving any application key. You don't need to generate an API key for this application.
We are opening the map intent from android.net.Uri. Immutable URI reference.
Implementation
- Open the Visual Studio Community edition and select Android App as below.
- Give the app name and click on "Next".
- Give the project and solution name and create the project.
The directory structure will be as follows.
Note
You may have to add the NewtonSoft.Json package for working on JSON serialize and deserialize objects.
Add the following file under the Model directory.
Add
- using System;
- namespace GoogleMapDemo.Model
- {
- public class Navigate
- {
- public string latitude { get; set; }
- public string longitude { get; set; }
- }
- }
Add the following content on main.axml.
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:paddingTop="20dp"
- android:paddingLeft="8dp"
- android:paddingRight="8dp">
- <EditText
- android:layout_width="fill_parent"
- android:textAppearance="?android:attr/textAppearanceMedium"
- android:layout_height="wrap_content"
- android:id="@+id/latitude"
- android:layout_marginBottom="15dp"
- android:hint="@string/latitude" />
- <EditText
- android:layout_width="fill_parent"
- android:textAppearance="?android:attr/textAppearanceMedium"
- android:layout_height="wrap_content"
- android:id="@+id/longitude"
- android:layout_marginTop="10dp"
- android:hint="@string/longitude" />
- <Button
- android:layout_width="fill_parent"
- android:textAppearance="?android:attr/textAppearanceMedium"
- android:layout_height="wrap_content"
- android:id="@+id/open_map_button"
- android:text="@string/open_map" />
- </LinearLayout>
Add the follwoing content on string.xml.
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="app_name">GoogleMapDemo</string>
- <string name="latitude">latitude</string>
- <string name="longitude">longitude</string>
- <string name="open_map">Open in Map</string>
- </resources>
Add the following content on MainActivity.cs
This activity is the main activity and it will have the screen that will take the user input. Clicking on button will take you to the new activity that will open the map.
- using Android.App;
- using Android.Content;
- using Android.OS;
- using System;
- using Android.Widget;
- using NavigateToLocation.Model;
- using Newtonsoft.Json;
-
- namespace NavigateToLocation
- {
- [Activity(Label = "NavigateToLocation", MainLauncher = true)]
- public class MainActivity : Activity
- {
- TextView _latitude, _longitude;
- Navigate _navigate;
-
- protected override void OnCreate(Bundle savedInstanceState)
- {
- base.OnCreate(savedInstanceState);
-
-
- SetContentView(Resource.Layout.Main);
-
- _navigate = new Navigate();
- _latitude = FindViewById<TextView>(Resource.Id.latitude);
- _longitude = FindViewById<TextView>(Resource.Id.longitude);
-
- FindViewById<Button>(Resource.Id.open_map_button).Click += MapButton_OnClick;
- }
-
- public void MapButton_OnClick(Object sender, EventArgs eventArgs)
- {
- _navigate.latitude = _latitude.Text;
- _navigate.longitude = _longitude.Text;
- var navigateIntent = new Intent(this, typeof(NavigateActivity));
- navigateIntent.PutExtra("navigate", JsonConvert.SerializeObject(_navigate));
- StartActivity(navigateIntent);
- }
- }
- }
Add file NavigateActivity.cs
This activity will open the map and contain the From and To fields filled when opening the map intent.
- using Android.App;
- using Android.Content;
- using Android.OS;
- using NavigateToLocation.Model;
- using Newtonsoft.Json;
-
- namespace NavigateToLocation
- {
- [Activity(Label = "NavigateToLocation")]
- public class NavigateActivity : Activity
- {
- Navigate _navigate;
- protected override void OnCreate(Bundle savedInstanceState)
- {
- base.OnCreate(savedInstanceState);
-
-
- _navigate = JsonConvert.DeserializeObject<Navigate>(Intent.GetStringExtra("navigate"));
- SetContentView(Resource.Layout.Main);
-
- var geoUri = Android.Net.Uri.Parse("http://maps.google.com/maps?daddr=+" + _navigate.latitude + "," + _navigate.longitude);
-
-
-
-
- var mapIntent = new Intent(Intent.ActionView, geoUri);
- StartActivity(mapIntent);
- }
-
- }
- }
You don't need to add any manifest information for any permission related information in file AndroidManifest.xml.
Screens
Execution
- Open the application.
- Give valid latitude and longitude.
- Click on the "Open in map" button.
- You will be redirected to map intent and you can have address From as source (i.e your current location) and destination as To address on the map. You can use navigation and direction accordingly.
- You can set the source address also. See the “cs” file for details.