protected GoogleResultSet GetResultSet(string addressText) { try { string requestURI = string.Format(GOOGLE_SEARCH_CHCH_URI, HttpUtility.UrlEncode(addressText), GOOGLE_API_KEY); string responseData = _webRequest.RetrieveContent(requestURI); GoogleResultSet resultSet = JSON.Deserialize<GoogleResultSet>(responseData); if (resultSet.Placemark == null) //failed to parse with boundingbox on Christchurch, fallback to more general { requestURI = string.Format(GOOGLE_SEARCH_FALLBACK_URI, HttpUtility.UrlEncode(addressText), GOOGLE_API_KEY); responseData = _webRequest.RetrieveContent(requestURI); resultSet = JSON.Deserialize<GoogleResultSet>(responseData); if (resultSet.Placemark != null && resultSet.Placemark.Length > 0) { GoogleResultSet.PlacemarkType placemark = resultSet.Placemark[0]; int accuracy = int.Parse(placemark.AddressDetails.accuracy); if (accuracy == 0) resultSet = reverseGeocodeCoordinates(addressText, placemark); } } return resultSet; } catch (Exception ex) { _log.ErrorException("Failure during parsing of address will just return null", ex); GoogleResultSet blankResult = new GoogleResultSet(); blankResult.name = addressText; GoogleResultSet.PlacemarkType dummyPlace=new GoogleResultSet.PlacemarkType(); dummyPlace.AddressDetails=new GoogleResultSet.PlacemarkType.AddressDetailsType(); dummyPlace.AddressDetails.accuracy = "0"; dummyPlace.Point=new GoogleResultSet.PlacemarkType.PointType(); dummyPlace.Point.coordinates=new decimal[2]; dummyPlace.Point.coordinates[0] = 0; dummyPlace.Point.coordinates[1] = 0; blankResult.Placemark= new GoogleResultSet.PlacemarkType[1]; blankResult.Placemark[0] = dummyPlace; //FIXME need to introduce an 'error occured' status.. //blankResult.Status = GoogleResultSet.StatusType.ERROR; return blankResult; } }
private GoogleResultSet reverseGeocodeCoordinates(string text, GoogleResultSet.PlacemarkType placemark) { string requestURI = string.Format(GOOGLE_REVERSE_URI, placemark.Point.coordinates[1],placemark.Point.coordinates[0]); string responseData = _webRequest.RetrieveContent(requestURI); //make sure the name isnt coordinates GoogleResultSet resultSet = JSON.Deserialize<GoogleResultSet>(responseData); resultSet.name = text; return resultSet; }
protected ILocation GetNewLocation(string addressText, string scopeLocation, GoogleResultSet resultSet) { ILocation newlyFound = Location.From(resultSet, scopeLocation); if(newlyFound==null) return null; //set the AddressText for the ID required by the repository SaveLocation(newlyFound, addressText); return newlyFound; }