CampusMapPage.xaml.cs CampusAppWP8::Pages::Campusmap::CampusMapPage CampusAppWP8::Pages::Campusmap System::Runtime::InteropServices::WindowsRuntime Windows::Networking::Proximity //----------------------------------------------------------------------------- //<copyrightfile="CampusMapPage.xaml.cs"company="BTU/IIT"> //Companycopyrighttag. //</copyright> //<author>fiedlchr</author> //<sience>13.08.2013</sience> //----------------------------------------------------------------------------- namespaceCampusAppWP8.Pages.Campusmap { usingSystem; usingSystem.Collections.Generic; usingSystem.Device.Location; usingSystem.Globalization; usingSystem.Linq; usingSystem.Runtime.InteropServices.WindowsRuntime; usingSystem.Threading; usingSystem.Windows; usingSystem.Windows.Controls; usingSystem.Windows.Input; usingSystem.Windows.Navigation; usingWindows.Networking.Proximity; usingCampusAppWP8.File.Places; usingCampusAppWP8.Model.Campusmap; usingCampusAppWP8.Model.GeoDb; usingCampusAppWP8.Resources; usingCampusAppWP8.Utility; usingCampusAppWP8.Utility.Lui.MessageBoxes; usingCampusAppWP8.Utility.NDEF; usingMicrosoft.Phone.Controls; usingMicrosoft.Phone.Shell; publicpartialclassCampusMapPage:PhoneApplicationPage { #regionMember privatereadonlyProximityDevicedevice=ProximityDevice.GetDefault(); privateCampusMapModelcampusMap; privatePlacesFilefile; privateList<string>informationsNames; privateboolqrcodeScan=false; privatestringlastQuery; privateboolbuildInfoEnable; privatelongndefId; #endregion #regionConstructor publicCampusMapPage() { this.InitializeComponent(); ApplicationBarMenuItemmenuItem1=ApplicationBar.MenuItems[0]asApplicationBarMenuItem; ApplicationBarMenuItemmenuItem2=ApplicationBar.MenuItems[1]asApplicationBarMenuItem; ApplicationBarMenuItemmenuItem3=ApplicationBar.MenuItems[2]asApplicationBarMenuItem; if(menuItem2!=null) { menuItem2.Text=AppResources.App_ScanQR; } if(menuItem3!=null) { menuItem3.Text=AppResources.App_ScanNfc; } if(menuItem1!=null) { menuItem1.Text=AppResources.CampusMapApp_EnableBuildInfo; } if(this.file==null) { this.file=newPlacesFile(); } this.file.OnLoaded+=newPlacesFile.OnIO(this.FileIsReady); this.file.LoadData(); } #endregion #regionEvents publicdelegatevoidLoadingPlace(stringargs); publiceventLoadingPlaceOnLoadingPlace; #endregion #regionMethod #regionprotected protectedoverridevoidOnNavigatedTo(NavigationEventArgse) { base.OnNavigatedTo(e); if(e.NavigationMode==NavigationMode.New) { this.ClearMap(); boolscroll=true; if(NavigationContext.QueryString.ContainsKey(Constants.ParamModelMap_SearchTermAlias)) { stringalias=NavigationContext.QueryString[Constants.ParamModelMap_SearchTermAlias]; this.ShowPlacesByQueryAsSearchPin(alias); scroll=false; if(Utilities.IsRoomId(alias)) { PlaceModelplace=this.file.Model.GetPlaceById(alias); if(place!=null) { this.GoToRoomList(place.ParentId,place.PlaceId); } } } else { this.ShowPlacesByQueryAsHiddenPin("campus"); } this.ShowCurrentPositionDispatcher(scroll); } elseif(this.qrcodeScan&&e.NavigationMode==NavigationMode.Back) { this.qrcodeScan=false; stringqrcodeResult=App.LoadFromIsolatedStorage<string>(Constants.CampusMapApp_QRCodeSearchResultStorageKey); App.SaveToIsolatedStorage<string>(Constants.CampusMapApp_QRCodeSearchResultStorageKey,null); stringsearchPid=Wp8StringManager.FilterPlaceIdinQRResultString(qrcodeResult); PlaceModelplace=this.file.Model.GetPlaceById(searchPid); if(searchPid!=null) { //this.ShowAllPlacesByPlaceIdAsSearchPin(searchPid); if(place.ParentId.Equals(this.campusMap.CampusId)||place.PlaceId.Equals(this.campusMap.CampusId)) { this.ShowAllPlacesByPlaceIdAsSearchPin(searchPid); } else { this.GoToRoomList(place.ParentId,place.PlaceId); } } } ndefId=this.device.SubscribeForMessage(Constants.NCFMessageType_NDEF,this.NDEFHandler); } protectedoverridevoidOnNavigatedFrom(NavigationEventArgse) { this.device.StopSubscribingForMessage(ndefId); base.OnNavigatedFrom(e); } #endregion #regionprivate privatevoidSetLastQuery(stringquery,Action<string>action) { this.lastQuery=query; this.OnLoadingPlace=newCampusMapPage.LoadingPlace(action); } privatevoidShowPlacesByQueryAsHiddenPin(stringquery) { if(!this.IsMapReady()) { this.SetLastQuery(query,this.ShowPlacesByQueryAsHiddenPin); } else { this.ShowPlacesByQuery(query,MapPinModel.PinType.Hidden,false); } } privatevoidShowPlacesByQueryAsSearchPin(stringquery) { if(!this.IsMapReady()) { this.SetLastQuery(query,this.ShowPlacesByQueryAsSearchPin); } else { boolallPlaces=false; if(Wp8StringManager.IsDigitsOnly(query)) { allPlaces=true; } this.ShowPlacesByQuery(query,MapPinModel.PinType.SearchPlace,allPlaces); } } privatevoidShowPlacesByQuery(stringquery,MapPinModel.PinTypepintype,boolallPlaces) { this.AddPins(this.SearchPlaces(query,allPlaces),pintype); } privatevoidShowPlacesByPlaceId(stringplaceId,MapPinModel.PinTypepintype,boolallPlaces) { this.AddPinsByPids(newList<string>(){placeId},pintype,false,allPlaces); } privatevoidShowAllPlacesByPlaceIdAsSearchPin(stringplaceId) { if(!this.IsMapReady()) { this.SetLastQuery(placeId,this.ShowAllPlacesByPlaceIdAsSearchPin); } else { this.ShowPlacesByPlaceId(placeId,MapPinModel.PinType.SearchPlace,true); } } privatevoidShowPlacesByPlaceIdAsSearchPin(stringplaceId) { if(!this.IsMapReady()) { this.SetLastQuery(placeId,this.ShowPlacesByPlaceIdAsSearchPin); } else { this.ShowPlacesByPlaceId(placeId,MapPinModel.PinType.SearchPlace,false); } } privatevoidShowAllCampusBuilding(stringcampusId) { if(!this.IsMapReady()) { this.SetLastQuery(campusId,this.ShowAllCampusBuilding); } else { this.AddPins(this.campusMap.Spatial.Places.ToList(),MapPinModel.PinType.InfoPlace,false); } } privateboolIsMapReady() { if(this.campusMap==null||!this.campusMap.IsReady) { returnfalse; } returntrue; } privatevoidFileIsReady() { this.campusMap=newCBMainMapModel(this.file.Model.Places.ToList()); this.MapCanvas.DataContext=this.campusMap; this.campusMap.ShowMapInfos+=newCBMainMapModel.MapInfos(this.ShowMapInfo); if(this.OnLoadingPlace!=null) { this.OnLoadingPlace(this.lastQuery); } } privatevoidSearchByText(objectsender,RoutedEventArgse) { stringquery=QString.Text.Trim(); if(query.Equals(string.Empty)) { return; } this.ClearMap(newList<string>(){MapPinModel.SearchPlacePinString}); this.ShowPlacesByQueryAsSearchPin(query); } privatevoidShowBuildingsInformation(objectsender,EventArgse) { stringmenuText; if(!this.buildInfoEnable) { this.ShowAllCampusBuilding(string.Empty); this.buildInfoEnable=true; menuText=AppResources.CampusMapApp_DisableBuildInfo; } else { this.ClearMap(newList<string>(){MapPinModel.InfoPlacePinString}); this.buildInfoEnable=false; menuText=AppResources.CampusMapApp_EnableBuildInfo; } ApplicationBarMenuItemitem=ApplicationBar.MenuItems[0]asApplicationBarMenuItem; item.Text=menuText; } privatevoidClearMap(List<string>removeTags=null) { if(removeTags==null) { MapCanvas.Children.Clear(); } else { List<UIElement>childs=MapCanvas.Children.ToList(); foreach(UIElementchildinchilds) { ImagechildImg=childasImage; if(childImg==null||childImg.Tag==null) { continue; } stringimgTag=childImg.Tag.ToString().Trim(); if(removeTags.Contains(imgTag)) { MapCanvas.Children.Remove(child); } } } } privatevoidAddPinsByPids(List<string>pidList,MapPinModel.PinTypepinType,boolclearCanvas=true,boolallPlaces=false) { //clearcanvas if(clearCanvas) { this.ClearMap(); } else { this.ClearMap(newList<string>(){MapPinModel.PinTypeToString(pinType)}); } //nullandemptylistassert if(pidList==null||pidList.Count<1) { return; } List<PlaceModel>placeList=newList<PlaceModel>(); PlaceModeltmpPlace; SpsModelspatial; if(allPlaces) { spatial=this.file.Model; } else { spatial=this.campusMap.Spatial; } foreach(stringpidinpidList) { tmpPlace=spatial.GetPlaceById(pid); if(tmpPlace!=null) { placeList.Add(tmpPlace); } } //addpinstomap if(placeList.Count>0) { this.AddPins(placeList,pinType); } } privatevoidSearchPlaceByNFC_Click(objectsender,EventArgse) { MessageBoxes.ShowMainModelInfoMessageBox(AppResources.ScarNfc_Search); this.DefHeader.ProgressVisibility=Visibility.Visible; this.device.StopSubscribingForMessage(this.ndefId); this.ndefId=this.device.SubscribeForMessage(Constants.NCFMessageType_NDEF,this.NDEFHandler); } privatevoidSearchPlaceByQR_Click(objectsender,EventArgse) { this.qrcodeScan=true; stringurlString=Constants.PathQR_QRPage; urlString+="?"+Constants.ParamQRResultKey+"="+Constants.CampusMapApp_QRCodeSearchResultStorageKey; Uriurl=newUri(urlStringasstring,UriKind.Relative); this.NavigationService.Navigate(url); } privatevoidNDEFHandler(ProximityDevicesender,ProximityMessagemessage) { //createndefMessage this.device.StopSubscribingForMessage(message.SubscriptionId); varndefMessage=message.Data; byte[]data=ndefMessage.ToArray(); NDEFMessagendef=newNDEFMessage(data); //searchforplaceId stringnfcContent=ndef.GetContent(); stringsearchPid=Wp8StringManager.FilterPlaceIdinNFCResultString(nfcContent.Trim()); PlaceModelplace=this.file.Model.GetPlaceById(searchPid); if(searchPid!=null) { //addpinstomap if(this.Dispatcher!=null) { if(place.ParentId.Equals(this.campusMap.CampusId)) { this.Dispatcher.BeginInvoke(newAction(()=>this.ShowAllPlacesByPlaceIdAsSearchPin(searchPid))); } else { this.Dispatcher.BeginInvoke(newAction(()=>this.GoToRoomList(place.ParentId,place.PlaceId))); } } else { //this.ShowAllPlacesByPlaceIdAsSearchPin(searchPid); if(place.ParentId.Equals(this.campusMap.CampusId)) { this.ShowAllPlacesByPlaceIdAsSearchPin(searchPid); } else { this.GoToRoomList(place.ParentId,place.PlaceId); } } } else { //Errorcase if(this.Dispatcher!=null) { this.Dispatcher.BeginInvoke(newAction(()=>MessageBoxes.ShowMainModelErrorMessageBox(AppResources.ScarNfc_Fail))); } else { MessageBoxes.ShowMainModelErrorMessageBox(AppResources.ScarNfc_Fail); } } if(this.Dispatcher!=null) { this.Dispatcher.BeginInvoke(newAction(()=>this.DefHeader.ProgressVisibility=Visibility.Collapsed)); } else { this.DefHeader.ProgressVisibility=Visibility.Collapsed; } ndefId=this.device.SubscribeForMessage(Constants.NCFMessageType_NDEF,this.NDEFHandler); } privateList<PlaceModel>SearchPlaces(stringquery,boolallPlaces=false) { SpsModelspatial; if(allPlaces) { spatial=this.file.Model; } else { spatial=this.campusMap.Spatial; } //ifqueryisanid if(Wp8StringManager.IsDigitsOnly(query)) { returnnewList<PlaceModel>(){spatial.GetPlaceById(query)}; } if(this.informationsNames==null) { this.informationsNames=newList<string>(); this.informationsNames.Add(Constants.PisInformationName_Name); this.informationsNames.Add(Constants.PisInformationName_Typ); this.informationsNames.Add(Constants.PisInformationName_ShortName); } returnspatial.GetPlacesByInformation(query,true,this.informationsNames); } privatevoidAddPins(List<PlaceModel>places,MapPinModel.PinTypetype,boolscroll=true) { foreach(PlaceModelplaceinplaces) { if(place==null) { continue; } GeoCoordinatecoor=place.GeoRefPoint; if(coor!=null) { List<PlaceModel>assocPlaces=newList<PlaceModel>(); assocPlaces.Add(place); this.AddPin(coor.Longitude,coor.Latitude,type,scroll,assocPlaces); } } } privatevoidAddPin(doublex,doubley,MapPinModel.PinTypetype,boolscroll=true,List<PlaceModel>assocPlaces=null) { PointscrollPoint=this.campusMap.GetScrollPoint(this.campusMap.ConverToPixelPoint(this.campusMap.ConverToMapPoint(x,y))); MapCanvas.Children.Add(this.campusMap.AddPinFromRefPoint(this.campusMap.ConverToPixelPoint(this.campusMap.ConverToMapPoint(x,y)),type,assocPlaces)); MapScroller.UpdateLayout(); if(scroll) { if(scrollPoint.X>this.campusMap.ImageWidth) { scrollPoint.X=this.campusMap.ImageWidth; } elseif(scrollPoint.X<0) { scrollPoint.X=0; } if(scrollPoint.Y>this.campusMap.ImageHeight) { scrollPoint.Y=this.campusMap.ImageHeight; } elseif(scrollPoint.Y<0) { scrollPoint.Y=0; } MapScroller.ScrollToVerticalOffset(scrollPoint.Y); MapScroller.ScrollToHorizontalOffset(scrollPoint.X); } } privatevoidUpdateButtonAppBar_Click(objectsender,System.EventArgse) { this.ShowCurrentPositionDispatcher(); } privatevoidShowCurrentPositionDispatcher(boolscroll=true) { this.DefHeader.ProgressVisibility=Visibility.Visible; Threadthread=newThread(delegate(){this.ShowCurrentPosition(scroll);}); thread.Start(); } privatevoidShowCurrentPosition(boolscroll=true) { if(Settings.AppSetting.GeoWatchEnable) { Utilities.DetermineAndStoreCurrentPositionForce(); if(this.Dispatcher!=null) { this.Dispatcher.BeginInvoke(newAction(()=>this.SetPinToCurrentPosition(scroll))); } else { this.SetPinToCurrentPosition(scroll); } } else { if(this.Dispatcher!=null) { this.Dispatcher.BeginInvoke(newAction(()=>this.DefHeader.ProgressVisibility=Visibility.Collapsed)); } else { this.DefHeader.ProgressVisibility=Visibility.Collapsed; } } } privatevoidSetPinToCurrentPosition(boolscroll=true) { stringlat=App.LoadFromAppState<string>(Constants.GeoWatch_CurrentPosition_Lat); stringlog=App.LoadFromAppState<string>(Constants.GeoWatch_CurrentPosition_Long); if(lat==null||log==null||(lat.Equals("0")&&log.Equals("0"))) { if(Settings.AppSetting.GeoWatchEnable) { MessageBoxes.ShowMainModelInfoMessageBox(AppResources.MsgBox_NoLocation); } this.DefHeader.ProgressVisibility=Visibility.Collapsed; } else { this.ClearMap(newList<string>(){MapPinModel.CurrendPositionPlacePinString}); this.SetPinToPosition(lat,log,MapPinModel.PinType.CurrentPosition,scroll); } } privatevoidSetPinToPosition(stringlatitude,stringlongitude,MapPinModel.PinTypetype,boolscroll=true) { doublex; doubley; if(!double.TryParse(longitude,NumberStyles.Any,CultureInfo.InvariantCulture,outx)||!double.TryParse(latitude,NumberStyles.Any,CultureInfo.InvariantCulture,outy)) { return; } this.AddPin(x,y,type,scroll); this.DefHeader.ProgressVisibility=Visibility.Collapsed; } privatevoidShowMapInfo(List<PlaceModel>places) { if(places==null) { return; } stringmsgText=string.Empty; foreach(PlaceModelplaceinplaces) { msgText+=AppResources.PlaceLabel_Name+":"; msgText+=place.GetInformationsValue(Constants.PisInformationName_Name); msgText=Wp8StringManager.AddNewLine(msgText); stringtype=place.GetInformationsValue(Constants.PisInformationName_Typ); if(type!=null) { msgText+=AppResources.PlaceLabel_Type+":"; msgText+=type; msgText=Wp8StringManager.AddNewLine(msgText); } stringshortDesc=place.GetInformationsValue(Constants.PisInformationName_ShortDesc); if(shortDesc!=null) { msgText+=AppResources.PlaceLabel_ShortDesc+":"; msgText+=shortDesc; msgText=Wp8StringManager.AddNewLine(msgText); } stringaccess=place.GetInformationsValue(Constants.PisInformationName_Accesbility); if(access!=null) { msgText+=AppResources.PlaceLabel_Accessbility+":"; msgText+=access; msgText=Wp8StringManager.AddNewLine(msgText); } } MessageBoxes.ShowMainModelInfoMessageBox(msgText); foreach(PlaceModelplaceinplaces) { if(place.ParentId.Equals(this.campusMap.CampusId)&&this.HasRooms(place.PlaceId)) { MessageBoxResultmsgResult=MessageBoxes.ShowPlaceInfoOkCancelMessageBox(AppResources.MsgBox_ShowRoomList); if(msgResult.Equals(MessageBoxResult.OK)) { this.GoToRoomList(place.PlaceId); return; } } } } privatevoidGoToRoomList(stringbuildingId,stringroomId=null) { stringurlString=Constants.PathCampusmap_RoomListPage; urlString+="?"+Constants.ParamBuildingId+"="+buildingId; if(roomId!=null) { urlString+="&"+Constants.ParamRoomId+"="+roomId; } Uriurl=newUri(urlStringasstring,UriKind.Relative); this.NavigationService.Navigate(url); } privateboolHasRooms(stringplaceId) { foreach(PlaceModelplaceinthis.file.Model.Places) { if(place.ParentId.Equals(placeId)) { returntrue; } } returnfalse; } privatevoidButton_KeyDown(objectsender,System.Windows.Input.KeyEventArgse) { if(e.Key==Key.Enter) { this.SearchByText(sender,e); this.MapScroller.Focus(); } } #endregion #endregion } }