Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ZXGeoResultParser.m 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * Copyright 2012 ZXing authors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "ZXGeoParsedResult.h"
  17. #import "ZXGeoResultParser.h"
  18. static NSRegularExpression *ZX_GEO_URL_PATTERN = nil;
  19. @implementation ZXGeoResultParser
  20. + (void)initialize {
  21. if ([self class] != [ZXGeoResultParser class]) return;
  22. ZX_GEO_URL_PATTERN = [[NSRegularExpression alloc] initWithPattern:@"geo:([\\-0-9.]+),([\\-0-9.]+)(?:,([\\-0-9.]+))?(?:\\?(.*))?"
  23. options:NSRegularExpressionCaseInsensitive error:nil];
  24. }
  25. - (ZXParsedResult *)parse:(ZXResult *)result {
  26. NSString *rawText = [ZXResultParser massagedText:result];
  27. if (rawText == nil || (![rawText hasPrefix:@"geo:"] && ![rawText hasPrefix:@"GEO:"])) {
  28. return nil;
  29. }
  30. NSArray *matches = [ZX_GEO_URL_PATTERN matchesInString:rawText options:0 range:NSMakeRange(0, rawText.length)];
  31. if (matches.count == 0) {
  32. return nil;
  33. }
  34. NSTextCheckingResult *match = matches[0];
  35. NSString *query = nil;
  36. if ([match rangeAtIndex:4].location != NSNotFound) {
  37. query = [rawText substringWithRange:[match rangeAtIndex:4]];
  38. }
  39. double latitude = [[rawText substringWithRange:[match rangeAtIndex:1]] doubleValue];
  40. if (latitude > 90.0 || latitude < -90.0) {
  41. return nil;
  42. }
  43. double longitude = [[rawText substringWithRange:[match rangeAtIndex:2]] doubleValue];
  44. if (longitude > 180.0 || longitude < -180.0) {
  45. return nil;
  46. }
  47. double altitude;
  48. if ([match rangeAtIndex:3].location == NSNotFound) {
  49. altitude = 0.0;
  50. } else {
  51. altitude = [[rawText substringWithRange:[match rangeAtIndex:3]] doubleValue];
  52. if (altitude < 0.0) {
  53. return nil;
  54. }
  55. }
  56. return [ZXGeoParsedResult geoParsedResultWithLatitude:latitude longitude:longitude altitude:altitude query:query];
  57. }
  58. @end