Advertisement
Guest User

MapView

a guest
Jul 4th, 2013
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.50 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. using Android.App;
  7. using Android.Content;
  8. using Android.Gms.Maps.Model;
  9. using Android.Gms.Maps;
  10. using Android.Graphics;
  11. using Android.Graphics.Drawables;
  12. using Android.OS;
  13. using Android.Runtime;
  14. using Android.Support.V4.App;
  15. using Android.Util;
  16. using Android.Views;
  17. using Android.Widget;
  18. using HyperLocal.Common;
  19.  
  20. namespace HyperLocal.Demo.Xamdroid
  21. {
  22.     public class MapFragment : Fragment, GoogleMap.IOnInfoWindowClickListener, GoogleMap.IOnCameraChangeListener, GoogleMap.IOnMarkerClickListener
  23.     {
  24.         List<MarkerInfo> pois = new List<MarkerInfo>();
  25.  
  26.         private Dictionary<string, MarkerInfo> currentMarkerInfos = new Dictionary<string, MarkerInfo>();
  27.         private MapView mapView;
  28.         private GoogleMap map;
  29.  
  30.         bool firstLoadWithLocation = true;
  31.         bool shouldAllowCameraChange = true;
  32.  
  33.         public override View OnCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle)
  34.         {
  35.             var view = layoutInflater.Inflate(Resource.Layout.MapDialogFragment, viewGroup, false);
  36.            
  37.  
  38.             mapView = view.FindViewById<MapView>(Resource.Id.map);
  39.  
  40.             mapView.OnCreate(bundle);
  41.  
  42.             try
  43.             {
  44.                 MapsInitializer.Initialize(this.Activity);
  45.             }
  46.             catch (Exception ex)
  47.             {
  48.                 Android.Util.Log.Error("ARDEMO", ex.ToString());
  49.             }
  50.  
  51.             map = mapView.Map;
  52.  
  53.             map.UiSettings.MyLocationButtonEnabled = true;
  54.  
  55.             map.SetLocationSource((MainActivity)this.Activity);
  56.  
  57.             map.SetOnCameraChangeListener (this);
  58.             map.SetOnInfoWindowClickListener(this);
  59.             map.SetOnMarkerClickListener (this);
  60.             Common.Log.Debug("MapFragment->OnCreateView");
  61.  
  62.  
  63.             var cu = CameraUpdateFactory.NewCameraPosition(new CameraPosition(new LatLng(PoiService.Instance.CurrentLatitude, PoiService.Instance.CurrentLongitude), 16, 0, 0));
  64.             map.MoveCamera(cu);
  65.  
  66.             return view;
  67.         }
  68.  
  69.         public bool OnMarkerClick(Marker marker)
  70.         {
  71.             shouldAllowCameraChange = false;
  72.             Common.Log.Debug ("MapFragment->MarkerClick");
  73.             return false;
  74.         }
  75.  
  76.         public void OnInfoWindowClick(Marker marker)
  77.         {
  78.             Common.Log.Debug ("MapFragment->OnInfoWindowClick");
  79.  
  80.             if (currentMarkerInfos.ContainsKey(marker.Id))
  81.             {
  82.                 Common.Log.Debug("Marker InfoWindow Clicked: " + marker.Id);
  83.  
  84.                 var markerInfo = currentMarkerInfos[marker.Id];
  85.                
  86.                 var ddf = new DetailsDialogFragment() { Item = markerInfo };
  87.                 ddf.Show(this.Activity.SupportFragmentManager, "MDF");
  88.             }
  89.            
  90.         }
  91.  
  92.         public async void OnCameraChange (CameraPosition cameraPos)
  93.         {
  94.             Common.Log.Debug ("MapFragment->CameraChange");
  95.  
  96.             if (!shouldAllowCameraChange) {
  97.                 shouldAllowCameraChange = true;
  98.                 return;
  99.             }
  100.  
  101.             var llb = map.Projection.VisibleRegion.LatLngBounds;
  102.             var visibleRadius = GeoMath.Distance (llb.Northeast.Latitude, llb.Northeast.Longitude, llb.Southwest.Latitude, llb.Southwest.Longitude, GeoMath.MeasureUnits.Kilometers);
  103.  
  104.             var radius = GeoMath.KmToDegrees (visibleRadius);
  105.  
  106.  
  107.             var lat = cameraPos.Target.Latitude;
  108.             var lng = cameraPos.Target.Longitude;
  109.  
  110.             var markers = await PoiService.Instance.SearchAsync (null, lat, lng, radius);
  111.  
  112.                 this.pois.Clear();
  113.                 this.pois.AddRange(markers);
  114.  
  115.             try { UpdatePois(); } catch { }
  116.         }
  117.  
  118.  
  119.         void UpdatePois()
  120.         {
  121.             this.Activity.RunOnUiThread(() => map.Clear());
  122.  
  123.             currentMarkerInfos.Clear();
  124.  
  125.             Console.WriteLine("POIS MAP VIEW: " + pois.Count());
  126.  
  127.             foreach (var p in pois)
  128.             {
  129.                 var mo = new MarkerOptions();
  130.                 mo.SetPosition(new LatLng(p.Latitude, p.Longitude));
  131.                 mo.SetTitle(p.Name);
  132.                 mo.SetSnippet(p.Category);
  133.  
  134.                 this.Activity.RunOnUiThread(() =>
  135.                 {
  136.                     var addedMarker = map.AddMarker(mo);
  137.  
  138.                     if (addedMarker == null)
  139.                         Console.WriteLine("Added Marker is null");
  140.                     else
  141.                         currentMarkerInfos.Add(addedMarker.Id, p);
  142.                 });
  143.             }
  144.         }
  145.  
  146.         public void MoveCamera(double latitude, double longitude)
  147.         {
  148.             firstLoadWithLocation = false;
  149.  
  150.             var cu = CameraUpdateFactory.NewCameraPosition(new CameraPosition(new LatLng(latitude, longitude), 16, 0, 0));
  151.             map.AnimateCamera(cu, 1, null);
  152.         }
  153.  
  154.         public override void OnResume()
  155.         {
  156.             Common.Log.Debug("MapFragment->OnResume");
  157.  
  158.             mapView.OnResume();
  159.             base.OnResume();
  160.  
  161.             map.MyLocationEnabled = true;
  162.  
  163.             UpdatePois();
  164.         }
  165.  
  166.         public override void OnDestroy()
  167.         {
  168.             Common.Log.Debug("MapFragment->OnDestroy");
  169.                
  170.             mapView.OnDestroy();
  171.             base.OnDestroy();
  172.         }
  173.  
  174.         public override void OnLowMemory()
  175.         {
  176.             mapView.OnLowMemory();
  177.             base.OnLowMemory();
  178.         }
  179.     }
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement