I have a Google mapping application that displays one or more markers on the map. I have successfully implemented a custom marker popup that displays details of the location the marker is pointing to. Now I need the application to, when the user presses the details box displayed when the marker is pressed, to launch a new activity that will open the web browser and display details of the location.
I have search the web to find a C# example of using the GoogleMap.OnInfoWindowClickListener which I believe is what I need to implement without any success.
I have spent all day on this and it feels that it should be simple, so I just missing something very obvious.
namespace BedfordBC { using Android.App; using Android.Gms.Maps; using Android.Gms.Maps.Model; using Android.OS; using Android.Widget; using System; using Android.Text; using Android.Text.Style; using Android.Views;
using System.Collections.Generic;
[Activity(Label = "@string/activity_label_mapwithmarkers")]
public class DisplayMap : Activity
{
private GoogleMap _map;
private MapFragment _mapFragment;
//BBC Map stuff
private List<MapItem> siteDetails = null; //new List<MapItem>();
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
//Get site details sent from calling screen either single site or
// multiple sites if Map button pressed in header of listview
string siteCount = Intent.GetStringExtra("SiteCount");
if (siteCount == null) {
siteDetails = LeisureListAdapter.SelMap; //Single button in list pressed
} else
siteDetails = LeisureServices.SelMap; //Map button at the top of the list selected
SetContentView(Resource.Layout.MapLayout);
InitMapFragment();
//SetupZoomInButton();
//SetupZoomOutButton();
}
protected override void OnResume()
{
base.OnResume();
SetupMapIfNeeded();
}
private void InitMapFragment()
{
_mapFragment = FragmentManager.FindFragmentByTag("map") as MapFragment;
if (_mapFragment == null)
{
GoogleMapOptions mapOptions = new GoogleMapOptions()
.InvokeMapType(GoogleMap.MapTypeNormal)
.InvokeZoomControlsEnabled(false)
.InvokeCompassEnabled(true);
FragmentTransaction fragTx = FragmentManager.BeginTransaction();
_mapFragment = MapFragment.NewInstance(mapOptions);
fragTx.Add(Resource.Id.map, _mapFragment, "map");
fragTx.Commit();
}
}
private void SetupMapIfNeeded()
{
if (_map == null)
{
_map = _mapFragment.Map;
if (_map != null)
{
//If Header Map button pressed there will be more than one site to add a marker for
foreach (var item in siteDetails) {
MarkerOptions markerOpt1 = new MarkerOptions();
markerOpt1.SetPosition(new LatLng(item.Latitude, item.Longitude));
markerOpt1.SetTitle(item.Name);
//string snippet = "Address 1 " + System.Environment.NewLine + "Address 2 " + System.Environment.NewLine + "Address 3";
SpannableString titleText = new SpannableString(item.Address);
//titleText.
markerOpt1.SetSnippet (titleText.ToString());
_map.AddMarker(markerOpt1);
}
// Custom Marker
_map.SetInfoWindowAdapter(new CustomMarkerPopupAdapter(LayoutInflater));
// We create an instance of CameraUpdate, and move the map to it.
CameraUpdate cameraUpdate = CameraUpdateFactory.NewLatLngZoom(new LatLng(siteDetails[0].Latitude, siteDetails[0].Longitude), 15);
_map.MoveCamera(cameraUpdate);
}
}
}
}
public class CustomMarkerPopupAdapter : Java.Lang.Object, GoogleMap.IInfoWindowAdapter
{
private LayoutInflater _layoutInflater = null;
public CustomMarkerPopupAdapter(LayoutInflater inflater)
{
_layoutInflater = inflater;
}
public View GetInfoWindow(Marker marker)
{
return null;
}
public View GetInfoContents(Marker marker)
{
var customPopup = _layoutInflater.Inflate(Resource.Layout.CustomMarkerPopup, null);
var titleTextView = customPopup.FindViewById<TextView>(Resource.Id.custom_marker_popup_title);
if (titleTextView != null)
{
titleTextView.Text = marker.Title;
}
var snippetTextView = customPopup.FindViewById<TextView>(Resource.Id.custom_marker_popup_snippet);
if (snippetTextView != null)
{
snippetTextView.Text = marker.Snippet;
}
return customPopup;
}
}
}