GoogleMaps / Places无效的API密钥错误Swift 3

时间:2017-08-23 03:26:14

标签: google-maps-api-3 swift3

更新:

我为GoogleMaps + GooglePlaces设置了一个项目并导入了API。我的问题是我的代码中不断出现无效的API密钥错误。我已经通过Google的教程应用程序检查了我的API,并且API密钥有效。我检查所述api密钥有效性的代码是坏的,或者我还没有完成API的设置。在我的Google控制台上的设置中,重命名我的api键是做什么的还是只是肤浅的?

选择单元格开始调用url / api

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let cell = collectionView.cellForItem(at: indexPath) as! Cell
    var id: String = String()
    var image: String = String()

    if (placeFiltered) {
        id = String(filteredPlaces[indexPath.row].components(separatedBy: ":").first!)
    } else {
        id = String(indexPath.row)
    }

    image = "Place Icon-" + id + " Selected"
    cell.placeIcon.image = UIImage(named: image)

    var placeType: String = String()

    if (placeFiltered) {
        placeType = String(filteredPlaces[indexPath.row].components(separatedBy: ":").last!)
    } else {
        placeType = String(Google.placeTypes[indexPath.row].components(separatedBy: ":").last!)
    }

    let type = (placeType.replacingOccurrences(of: " ", with: "_")).lowercased()
    let location = String(latitude) + "," + String(longitude)

    GoogleClient.sharedInstance().fetchNearbyPlaces(of: type, for: location) { (success, error) in

        GCDBlakBox {

            self.searchBar.resignFirstResponder()
            self.searchBarCancelButtonClicked(self.searchBar)

            if success {

                self.dropAllData() { (success, error) in
                    if success {

                        self.saveType(placeType: placeType) { (result) in
                            if (result != nil) {

                                self.savePlace(result!) { (result) in
                                    if (result != nil) {

                                        self.performSegue(withIdentifier: "toPlaceTabBarController", sender: placeType)
                                    } else {
                                        self.displayError("Error saving search result")
                                    }
                                }
                            } else {
                                self.displayError("Error saving search result")
                            }
                        }
                    } else {
                        self.displayError(error)
                    }
                }
            } else {
                var errorMessage: String = error!

                if (errorMessage == Google.ResponseValues.zeroResults) {
                    if (self.appDelegate.searchNearby) {
                        errorMessage = "There is no " + (placeType).lowercased() + " nearby within 30 miles"
                    } else {
                        errorMessage = "There is no " + (placeType).lowercased() + " nearby. Please increase the radius and search again"
                    }
                }

                self.displayError(errorMessage)
            }
        }
    }
}

然后调用此函数

func fetchNearbyPlaces(of type: String, for location: String, completionHandlerForFetchPlaces: @escaping (_ success: Bool, _ error: String?) -> Void) {

        UIApplication.shared.isNetworkActivityIndicatorVisible = true

        var parameters: [String:Any] = [
            Google.ParameterKeys.type: type,
            Google.ParameterKeys.location: location,
            Google.ParameterKeys.key: Google.apiKey
        ]

        let searchNearby = appDelegate.searchNearby
        let searchRadius = appDelegate.searchRadius

        if (searchNearby) {
            parameters[Google.ParameterKeys.rankby] = Google.ParameterValues.distance
        } else {
            parameters[Google.ParameterKeys.rankby] = Google.ParameterValues.prominence
            parameters[Google.ParameterKeys.radius] = searchRadius
        }

        let request = NSMutableURLRequest(url: urlFrom(parameters, withPathExtension: Google.URL.output))

        let _ = taskForData(request) { (success, error, result) in

            guard (error == nil) else {
                completionHandlerForFetchPlaces(false, error)
                return
            }

            if success {

                guard let status = result?[Google.ResponseKeys.status] as? String else {
                    completionHandlerForFetchPlaces(false, "Place search failed")
                    return
                }

                if (status == Google.ResponseValues.ok) {
                    if (self.appDelegate.googlePlaces.count != 0) {
                        self.appDelegate.googlePlaces.removeAll()
                    }

                    let results = result?[Google.ResponseKeys.results] as! Array<NSDictionary>
                    self.appDelegate.googlePlaces = GooglePlaces.allPlacesFrom(results)
                    completionHandlerForFetchPlaces(true, nil)
                } else if (status == Google.ResponseValues.zeroResults) {
                    completionHandlerForFetchPlaces(false, status)
                } else if (status == Google.ResponseValues.overQueryLimit) {
                    completionHandlerForFetchPlaces(false, "Daily search limit exceeded. Try again tomorrow")
                } else if (status == Google.ResponseValues.requestDenied) {
                    completionHandlerForFetchPlaces(false, "Request denied. Invalid API key") // HERE IS WHERE I GET MY ERROR !!!!!!! 
                } else if (status == Google.ResponseValues.invalidRequest) {
                    completionHandlerForFetchPlaces(false, "Invalid request")
                }
            }
        }
    }
}

GIT: https://github.com/whitesoxmike/Tourist-Error.git

更新:我做了一个简化的可验证项目

0 个答案:

没有答案