Swift: Geofencing / geolocations near user location

一世执手 提交于 2019-12-04 14:14:46

First and foremost, change back startMonitoringGeotification(), regionWithGeotification(), and stopMonitoringGeotification() to take in a Geotification like the Ray Wenderlich tutorial. Make sure you have added the file Geotification.swift from his starter code to your project.

Also, make sure your Main.storyboard launches your ViewController. Without this step, none of your code will run.

1) redefine your Locations class more simply in Locations.swift:

import UIKit
import MapKit

class Locations {

  static let locations:[String:CLLocationCoordinate2D] = [
    "buddyrogers" : CLLocationCoordinate2D(latitude: 33.815995425565184, longitude: -116.44107442645873),
    "diamond"     : CLLocationCoordinate2D(latitude: 33.802168595307814, longitude: -116.45711048941041),
     . 
     .    // add your locations
     .
    ]
  }
}

like the @hungry-yeti suggested

2) You can define showSimpleAlertWithTitle() in your GeotificationViewController class. Try calling it in your ViewDidLoad() to test it. You can now delete Utilities.swift.

3) I think you can ignore/remove UserLocation.swift, this seems unnecessary

4) Put this code inside GeotificationViewController's ViewDidLoad:

let radius = CLLocationDistance(8046.72) // 5 miles in meters

for location in Locations.locations {
  let g = Geotification(coordinate: location.1, radius: radius, identifier: location.0, note: "test", eventType: EventType.OnEntry)
    startMonitoringGeotification(g)
}

5) I hope this helps and simplifies your code. Party on, reply here if you have any issues.

It looks like you're using the Ray Wenderlich tutorial. That's a good one, I found it very useful too.

First off, the unit for CLLocationDistance is meters so the code you have specifies a radius of 5 meters which won't be quite as useful as you may hope; a value of 8046.72 is closer to 5 miles.

Regarding the specific error, Locations is the class that you stuffed all CLLocationCoordinate2D values in, it certainly does not have any member called coordinate. If you're using the tutorial I'm thinking of you will need to load those coords into instances of the Geotification class.

Here is some untested code:

  // Load the various coords into an array:
  var locations:[(note:String, coords:CLLocationCoordinate2D)] = []
  locations +=[(note: "arroyo", CLLocationCoordinate2D( latitude: 33.781327997137595, longitude: -116.46394436519012)]
  locations +=[(note: "buddyrogers", CLLocationCoordinate2D( latitude: 33.78051204742721, longitude: -116.46362250010833)]
  // ...

  let radius = 8000  // ~5 miles rounded to nearest km
  // Load the locations into geotifications:
  for location in locations {
     let geotification = Geotification(coordinate: location.cords, radius: radius, identifier: NSUUID().UUIDString, note: location.note, eventType: EventType.OnEnter)
     startMonitoringGeotification(geotification)
  }

Now bear in mind that there is a hard limit of 20 monitored regions per app, so if you have more than that you will need to dynamically determine the nearest 20 regions and then monitor those regions.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!