public async Task CreateGeofence(List <List <double> > polygonCoordinates) { var geometry = new UploadGeofenceGeometryModel(); geometry.Coordinates.Add(polygonCoordinates); await this.UploadGeofence(geometry); }
public async Task CreateGeofence() { // If Geofence is created do nothing if (!string.IsNullOrEmpty(_currentGeofenceUdId)) { return; } var currentPosition = new GeoCoordinateModel(Constants.FakeUserDefaultLatitude, Constants.FakeUserDefaultLongitude); var firstPosition = currentPosition.CalculateDerivedPosition(Constants.GeofenceRatioInMeters, -90); var secondPosition = currentPosition.CalculateDerivedPosition(Constants.GeofenceRatioInMeters, 0); var thirdPosition = currentPosition.CalculateDerivedPosition(Constants.GeofenceRatioInMeters, 90); var fourthPosition = currentPosition.CalculateDerivedPosition(Constants.GeofenceRatioInMeters, 180); var polygonCoordinates = new List <List <double> > { new List <double> { firstPosition.Longitude, firstPosition.Latitude }, new List <double> { secondPosition.Longitude, secondPosition.Latitude }, new List <double> { thirdPosition.Longitude, thirdPosition.Latitude }, new List <double> { fourthPosition.Longitude, fourthPosition.Latitude }, new List <double> { firstPosition.Longitude, firstPosition.Latitude } }; var geometry = new UploadGeofenceGeometryModel(); geometry.Coordinates.Add(polygonCoordinates); await this.UploadGeofence(geometry); }
private async Task <GeofenceResourceLocationResponseModel> UploadGeofence(UploadGeofenceGeometryModel geometry) { await RemoveCurrentGeofence(); //Remove first if exists previous var now = DateTime.UtcNow; var properties = new UploadGeofencePropertiesModel(); var uploadGeofenceFeatureModel = new UploadGeofenceFeatureModel { Geometry = geometry, Properties = properties }; var features = new List <UploadGeofenceFeatureModel>() { uploadGeofenceFeatureModel }; UploadGeofenceModel model = null; model = new UploadGeofenceModel { Features = features }; var serializerSettings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }; var body = JsonConvert.SerializeObject(model, serializerSettings); var content = new ByteArrayContent(Encoding.UTF8.GetBytes(body)); var requestUri = $"{_azureMapsOptions.ApiEndpoint}/mapData/upload?api-version={_azureMapsOptions.ApiVersion}&subscription-key={_azureMapsOptions.Key}&dataFormat=geojson"; content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); var responseMessage = await Client.PostAsync(requestUri, content); responseMessage.EnsureSuccessStatusCode(); var locationHeader = responseMessage.Headers.FirstOrDefault(header => string.Equals(header.Key, "Location", StringComparison.OrdinalIgnoreCase)); if (locationHeader.Key == null) { throw new Exception("Can't get the Location header to know the status of the Geofence upload."); } // The Data Upload API is a long-running request with the following sequence of operations: // 1. Send the upload request // 2. Upload request "Accepted" but "In Progress" // 3. Upload request "Completed" successfully // We need to get the status until is Completed or we try to many times to unblock the process but should never happen. var tries = 0; while (tries < 100) { var uploadStatus = await GetUploadGeofenceStatus(locationHeader.Value.FirstOrDefault()); if (uploadStatus != null && string.Equals(uploadStatus.Status, Constants.SucceededStatus)) { var resourceLocationResponse = await GetResourceLocation(uploadStatus.ResourceLocation); _currentGeofenceUdId = resourceLocationResponse.Udid; return(resourceLocationResponse); } else { await Task.Delay(200); // Wait 200 milliseconds before try again } tries++; } throw new Exception("Can't upload geofence."); }