Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google maps 使用谷歌地图获取附近的地方_Google Maps_Swift_Ios8_Google Places Api_Google Maps Sdk Ios - Fatal编程技术网

Google maps 使用谷歌地图获取附近的地方

Google maps 使用谷歌地图获取附近的地方,google-maps,swift,ios8,google-places-api,google-maps-sdk-ios,Google Maps,Swift,Ios8,Google Places Api,Google Maps Sdk Ios,我正在尝试实现使用谷歌地图查找我当前位置附近的位置。 我在谷歌开发者控制台中创建了一个项目,并获得了“ios APIkey”和“服务器APIkey”。我还为iOS启用了Google Places API和Google Maps SDK。 下面是查找附近位置的代码 func fetchPlacesNearCoordinate(coordinate: CLLocationCoordinate2D, radius: Double, name : String){ var urlString =

我正在尝试实现使用谷歌地图查找我当前位置附近的位置。 我在谷歌开发者控制台中创建了一个项目,并获得了“ios APIkey”和“服务器APIkey”。我还为iOS启用了Google Places API和Google Maps SDK。 下面是查找附近位置的代码

func fetchPlacesNearCoordinate(coordinate: CLLocationCoordinate2D, radius: Double, name : String){
    var urlString = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?key=\(apiServerKey)&location=\(coordinate.latitude),\(coordinate.longitude)&radius=\(radius)&rankby=prominence&sensor=true"
    urlString += "&name=\(name)"

    urlString = urlString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!
    println(urlString)
    if placesTask.taskIdentifier > 0 && placesTask.state == .Running {
        placesTask.cancel()

    }
    UIApplication.sharedApplication().networkActivityIndicatorVisible = true
    placesTask = session.dataTaskWithURL(NSURL(string: urlString)!) {data, response, error in
        println("inside.")
        UIApplication.sharedApplication().networkActivityIndicatorVisible = false
        if let json = NSJSONSerialization.JSONObjectWithData(data, options:nil, error:nil) as? NSDictionary {
            if let results = json["results"] as? NSArray {
                for rawPlace:AnyObject in results {
                    println(rawPlace)
                    self.results.append(rawPlace as! String)
                }
            }
        }
            self.placesTask.resume()
    }
}

传递的坐标是当前位置的坐标。若我执行上面的代码,并没有发生任何事情,但生成的url是有效的。如果我把这个网址放进谷歌,我会得到正确的结果。但我的应用程序中没有显示任何内容。请帮我解决这个问题。请让我知道我错在哪里

您将位置添加到结果数组中,但您已经完成了任何更新地图视图的操作,例如,将标记添加到地图视图中

将标记添加到mapview的示例代码:

   let returnedPlaces: NSArray? = jsonResult["results"] as? NSArray

  if returnedPlaces != nil {

           for index in 0..<returnedPlaces!.count {

                 if let returnedPlace = returnedPlaces?[index] as? NSDictionary {

                       var placeName = ""
                       var latitude = 0.0
                       var longitude = 0.0

                       if let name = returnedPlace["name"] as? NSString {
                           placeName = name as String
                       }

                       if let geometry = returnedPlace["geometry"] as? NSDictionary {
                           if let location = geometry["location"] as? NSDictionary {
                                 if let lat = location["lat"] as? Double {
                                            latitude = lat
                                 }

                                 if let lng = location["lng"] as? Double {
                                           longitude = lng
                                  }
                            }
                       }

                       let marker = GMSMarker()
                       marker.position = CLLocationCoordinate2DMake(latitude, longitude)
                       marker.title = placeName
                       marker.map = self.mapView
                }
           }
   }
let returnedPlaces:NSArray?=jsonResult[“结果”]作为?不可变数组
如果返回的位置!=零{

对于0..中的索引,只需设置Latt、long和Googleapkey

NSString *urlString= [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=%@,%@&radius=2000&key=%@",lati,longi,kGoogleAPIKey];
[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError)
     {
         if (connectionError)
         {
             NSLog(@"Error");
         }
         else
         {
             NSDictionary *dictJson = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
             NSArray *arrayPlaces = [[NSArray alloc] initWithArray:[dictJson objectForKey:@"results"]];
             NSLog("Place %@",arrayPlaces);

         }
     }];
此外,如果您想通过文本搜索位置,只需按如下所示创建urlString

  NSString *urlString = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/autocomplete/json?sensor=true&key=%@&language=en&input=%@&query=123+main+street&radius=50000&location=0.0,0.0&rankby=distance",kGoogleAPIKey,searchText];

在arrayPlaces中,您有附近的位置。

您的请求URL是什么?还有您的
self.placesTask.resume()
应该在你的
dataTaskWithURL
方法闭包之外。哦,是的!!!!非常感谢Ztan…是的,我将dataTaskWithURL放在了dataTaskWithURL中,这是一个愚蠢的错误…现在它工作正常了…再次感谢!!!我遵循了上面给出的教程,但是地图上没有加载这些位置。有人能帮我吗?我已经安装了如果我想搜索附近的库怎么办?
func fetchPlacesNearCoordinate(coordinate: CLLocationCoordinate2D, radius: Double, name : String){
    var urlString = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?key=\(apiServerKey)&location=\(coordinate.latitude),\(coordinate.longitude)&radius=\(radius)&rankby=prominence&sensor=true"
    urlString += "&name=\(name)"

    urlString = urlString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!
    println(urlString)
    if placesTask.taskIdentifier > 0 && placesTask.state == .Running {
        placesTask.cancel()

    }
    UIApplication.sharedApplication().networkActivityIndicatorVisible = true
    placesTask = session.dataTaskWithURL(NSURL(string: urlString)!) {data, response, error in
        println("inside.")
        UIApplication.sharedApplication().networkActivityIndicatorVisible = false
        if let json = NSJSONSerialization.JSONObjectWithData(data, options:nil, error:nil) as? NSDictionary {
            if let results = json["results"] as? NSArray {
                for rawPlace:AnyObject in results {
                    println(rawPlace)
                    self.results.append(rawPlace as! String)
                }
            }
        }
            self.placesTask.resume()
    }
}