我有这个程序,它会随机选择一个状态。假设它是纽约,我将如何取名纽约,然后按一个按钮在地图上进行搜索。我不希望用户键入它,而是希望它输入,所以我按下按钮进行搜索。我已经有地图设置,但是我在这部分上遇到了麻烦。

    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" {



        }
    }
}

最佳答案

使用此功能获取您已通过的地址的纬度

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的帮助下将其显示在地图中。

07-26 09:38