我如何获取一个地方的名称,然后使用MKMapView搜索它?

时间:2015-08-27 02:26:42

标签: ios swift mkmapview mapkit

我有这个应用程序,它随机选择一个州。让我们说它的纽约,我如何取名纽约并按下按钮在地图中搜索它。我不希望用户必须输入它,我希望它是这样我按下按钮然后搜索。我已经有了地图设置,但我遇到了这个部分的问题。

    func randomStateGenerator() {
    var randomState = Int(arc4random() % 50)
    println(randomState)

    if randomState == 0 {

            println("NEW YORK")


        newyork = SKLabelNode(fontNamed: "Happy Phantom")
        newyork.text = "newyork"
        newyork.zPosition = 22
        newyork.fontSize = 25
        newyork.fontColor = SKColor.blackColor()
        newyork.position = CGPointMake(self.size.width / 2.0, self.size.height / 1.8)
        self.addChild(newyork)



            }

    }

    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {

    for touch in (touches as! Set<UITouch>) {
        var touch: UITouch = touches.first as! UITouch
        var location = touch.locationInNode(self)
        var node = self.nodeAtPoint(location)


        if node.name == "find" {



        }
    }
}

1 个答案:

答案 0 :(得分:1)

使用此功能获取已通过的地址的Lat Long

func geoCodeUsingAddress(address: String) -> CLLocationCoordinate2D {
    var latitude: Double = 0
    var longitude: Double = 0
    var esc_addr: String = address.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
    var req: String = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=\(esc_addr)"
    var result: String = NSString.stringWithContentsOfURL(NSURL.URLWithString(req), encoding: NSUTF8StringEncoding, error: nil)
    if result {
        var scanner: NSScanner = NSScanner.scannerWithString(result)
        if scanner.scanUpToString("\"lat\" :", intoString: nil) && scanner.scanString("\"lat\" :", intoString: nil) {
            scanner.scanDouble(&latitude)
            if scanner.scanUpToString("\"lng\" :", intoString: nil) && scanner.scanString("\"lng\" :", intoString: nil) {
                scanner.scanDouble(&longitude)
            }
        }
    }
    var center: CLLocationCoordinate2D
    center.latitude = latitude
    center.longitude = longitude
    return center
}

此函数将返回lat long,借助lat long在地图中显示它。