Ios 如何在放大时显示地图注释?
我有一张地图,上面有很多注释,都钉在全国的地图上。现在我想在有人放大地图时自动显示注释。你知道怎么开始吗?现在我有几行代码: 注释类 地图视图类Ios 如何在放大时显示地图注释?,ios,swift,mkmapview,mkannotation,Ios,Swift,Mkmapview,Mkannotation,我有一张地图,上面有很多注释,都钉在全国的地图上。现在我想在有人放大地图时自动显示注释。你知道怎么开始吗?现在我有几行代码: 注释类 地图视图类 有什么想法吗?您可以通过调用mapView以编程方式选择任何注释,或更靠近地图中心的注释。选择注释(注释,动画:true)。但一次只能选择一个批注。我会这样做: extension Double{ func between( a:Double, b:Double) -> Bool{ if self > a &am
有什么想法吗?您可以通过调用
mapView以编程方式选择任何注释,或更靠近地图中心的注释。选择注释(注释,动画:true)
。但一次只能选择一个批注。我会这样做:
extension Double{
func between( a:Double, b:Double) -> Bool{
if self > a && self < b{
return true
}
return false
}
}
extension MapViewController : MKMapViewDelegate{
//using special image for standard view for the annotation
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
var anView = mapView.dequeueReusableAnnotationViewWithIdentifier("wtg")
if anView == nil {
anView = MKAnnotationView(annotation: annotation, reuseIdentifier: "wtg")
anView!.canShowCallout = true
anView!.image = UIImage(named: "wtg")
}
else {
anView!.annotation = annotation
}
return anView
}
//if the region center is close to something, show the annotations
func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
let region = mapView.region
if region.center.latitude.between(myLatA, b: mylatB) && region.center.longitude.between(myLonA, b: myLonB)
{
//Add extra anotations or do other stuff
}
}
}
双分机{
func-between(a:Double,b:Double)->Bool{
如果self>a&&selfMKAnnotationView{
var anView=mapView.dequeueReusableAnnotationViewWithIdentifier(“wtg”)
如果anView==nil{
anView=MKAnnotationView(注释:注释,重用标识符:“wtg”)
anView!.canShowCallout=true
anView!.image=UIImage(名为:“wtg”)
}
否则{
anView!.annotation=annotation
}
返回视图
}
//如果区域中心靠近某个对象,则显示注释
func地图视图(地图视图:MKMapView,区域IDChangeAnimated:Bool){
让region=mapView.region
if region.center.lation.between(myLatA,b:mylatB)和region.center.longitude.between(myLonA,b:myLonB)
{
//添加额外的涂鸦或做其他事情
}
}
}
苹果在这个问题上做得很好。您应该查看“显示多个注释对象”部分,并使用代理方法mapView:regionWillChangeAnimated:和mapView:regionDidChangeAnimated:方法
class MapViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
/* OUTLETS */
@IBOutlet weak var mapOutlet: MKMapView!
@IBOutlet weak var activityIndicator: UIActivityIndicatorView!
/* VARIABLES */
let lm = CLLocationManager()
var data = [[String:String]]()
override func viewDidLoad() {
super.viewDidLoad()
// pin's data is loaded from the server and added to `data`
}
override func viewDidAppear(animated: Bool) {
self.addAnnotationToTheMap()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func addAnnotationToTheMap(){
for i in data{
let latitude = Double(i["latitude"]!)
let longtitude = Double(i["longitude"]!)
let coordinates = CLLocationCoordinate2DMake(latitude!, longtitude!)
let pinOnTheMap = myAnnotation(title: i["title"]!, subtitle: i["subtitle"]!, coordinates: coordinates)
mapOutlet.addAnnotation(pinOnTheMap)
}
}
} // end of class
extension Double{
func between( a:Double, b:Double) -> Bool{
if self > a && self < b{
return true
}
return false
}
}
extension MapViewController : MKMapViewDelegate{
//using special image for standard view for the annotation
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
var anView = mapView.dequeueReusableAnnotationViewWithIdentifier("wtg")
if anView == nil {
anView = MKAnnotationView(annotation: annotation, reuseIdentifier: "wtg")
anView!.canShowCallout = true
anView!.image = UIImage(named: "wtg")
}
else {
anView!.annotation = annotation
}
return anView
}
//if the region center is close to something, show the annotations
func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
let region = mapView.region
if region.center.latitude.between(myLatA, b: mylatB) && region.center.longitude.between(myLonA, b: myLonB)
{
//Add extra anotations or do other stuff
}
}
}