2016-04-10 1 views
0

Ich erstelle eine App, die Pins, die ich hinzugefügt habe, dass, wenn geklickt öffnet eine Warnung, die Informationen über das Gebäude hat. Ich möchte die Nutzer nach diesen Standorten suchen lassen, bin mir aber nicht sicher, ob das möglich ist, weil ich keinen Code gefunden habe. Bitte lassen Sie mich wissen, wenn Sie wissen, wie Sie dies tun oder Anregungen haben.Ist es für Benutzer möglich, eine Karte zu suchen, um Orte zu sehen, die von mir in swift

import UIKit 
import MapKit 
import CoreLocation 

class CustomPointAnnotiation: MKPointAnnotation { 
    var imageName: String! 
} 

class MapVC: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate { 
    @IBOutlet weak var mapView: MKMapView! 

    var manager = CLLocationManager() 

    class Location: NSObject, MKAnnotation { 
     var title: String? 
     var coordinate: CLLocationCoordinate2D 
     var info: String 

     init(title: String, coordinate: CLLocationCoordinate2D, info: String) { 
      self.title = title 
      self.coordinate = coordinate 
      self.info = info 
        } 
    } 

    func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { 
     // 1 
     let identifier = "Location" 

     // 2 
     if annotation.isKindOfClass(Location.self) { 
      // 3 
      var image: UIImageView 
      image = UIImageView(frame:CGRect(x: 50,y: 200,width: 250,height: 180)) 
      image.image = UIImage(named:"") 
      self.view.addSubview(image) 

      var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier) 

      if annotationView == nil { 
       //4 
       annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier) 
       annotationView!.canShowCallout = true 

       // 5 
       let btn = UIButton(type: .DetailDisclosure) 
       annotationView!.rightCalloutAccessoryView = btn 
      } else { 
       // 6 
       annotationView!.annotation = annotation 
      } 
      return annotationView 
     } 

     // 7 
     return nil 
    } 

    func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) { 
     let location = view.annotation as! Location 
     let placeName = location.title 
     let placeInfo = location.info 

     var imageView: UIImageView 
     imageView = UIImageView(frame:CGRect(x: 50,y: 200,width: 250,height: 180)) 
     imageView.backgroundColor = UIColor.whiteColor() 
     imageView.layer.cornerRadius = 8.0 
     imageView.clipsToBounds = true 

     if location.title == "Woodburn Hall" { 
      imageView.image = UIImage(named:"woodburn.png") 
      self.view.addSubview(imageView) 
     } 
     let ac = UIAlertController(title: placeName, message:placeInfo, preferredStyle:UIAlertControllerStyle.ActionSheet) 
     ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil)) 

     self.presentViewController(ac, animated: true, completion: nil) 

     imageView.removeFromSuperview() 
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     mapView.delegate = self 

     // Core Location 
     manager.delegate = self 
     manager.desiredAccuracy = kCLLocationAccuracyBest 
     manager.requestWhenInUseAuthorization() 
     manager.startUpdatingLocation() 

     //set initial location to Indiana University 
     let initialLocation = CLLocation(latitude: 39.17, longitude: -86.5148) 
     let regionRadius: CLLocationDistance = 100 

     func centerMapOnLocation(location:CLLocation){ 
      let coordinateRegion = 
      MKCoordinateRegionMakeWithDistance(location.coordinate, regionRadius * 2.0, regionRadius * 2.0) 
      mapView.setRegion(coordinateRegion, animated: true) 


      mapView.mapType = MKMapType.Hybrid; 
     } 

     centerMapOnLocation(initialLocation) 

     let unionLocation = Location(title: "Memorial Union", coordinate: CLLocationCoordinate2D(latitude: 39.167541, longitude: -86.522387), info: "Address: 900 E 7th St, Bloomington, IN 47408 \n The student union on the campus. Was dedicated in June of 1932. As of date, it is the second largest student union, worldwide. Has multiple stages, offices, directors offices, and even a hotel in the building.") 

     mapView.addAnnotation(unionLocation) 

     let ashtonLocation = Location(title: "Ashton Residence Hall", coordinate:CLLocationCoordinate2D(latitude: 39.171550, longitude: -86.509768), info: "Address: 1800 E 10th St, Bloomington, IN 47408 \n Includes single dorms only and is located in central neighborhood.") 

     mapView.addAnnotation(ashtonLocation) 

Antwort

0

Es würde viel helfen, wenn Sie uns sagen könnten, wie oder wo Sie diese Stifte speichern. Haben Sie ein Backend, dass Sie Ihre Pins dynamisch hinzufügen oder sind sie in Ihrer Anwendung "fest codiert"? Zunächst benötigen Sie einen Speichermechanismus, damit die anderen Teile Ihres Codes diese Daten erreichen können.

Wenn Sie sie mithilfe einer API dynamisch in einer Datenbank speichern, besteht der beste Weg darin, eine Methode zu entwickeln, die Ihre Suchparameter übernimmt und die Ergebnisse zurückgibt.

Wenn die Pins in einer Datenbank oder Datei gespeichert werden, die mit Ihrer Anwendung geliefert wird, benötigen Sie geeignete Methoden, um diese Pins aus beliebiger Datei, Format oder Datenbank abzufragen.

Verwandte Themen