Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/35.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
纬度及;ios中自动完成地点搜索google API的经度_Ios_Iphone_Google Maps_Google Api - Fatal编程技术网

纬度及;ios中自动完成地点搜索google API的经度

纬度及;ios中自动完成地点搜索google API的经度,ios,iphone,google-maps,google-api,Ios,Iphone,Google Maps,Google Api,我从Google实现了自动完成地点搜索API。它很好用 但是在回应中,谷歌并没有返回这个地方的“纬度和经度” 我的代码如下所示 NSString *strUrl = [[[NSString stringWithFormat:@"%@%@%@?sensor=false&key=%@&input=%@", PLACES_API_BASE, TYPE_AUTOCOMPLETE, OUT_JSON,API_KEY, txt_search.text] strin

我从Google实现了自动完成地点搜索API。它很好用

但是在回应中,谷歌并没有返回这个地方的“纬度和经度”

我的代码如下所示

NSString *strUrl =
[[[NSString stringWithFormat:@"%@%@%@?sensor=false&key=%@&input=%@",
   PLACES_API_BASE,
   TYPE_AUTOCOMPLETE,
   OUT_JSON,API_KEY,
   txt_search.text]
  stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
 stringByReplacingOccurrencesOfString:@" " withString:@"%20"];


NSLog(@"--- address ---  %@",strUrl);

NSData *responseData = [NSData dataWithContentsOfURL:[NSURL URLWithString:strUrl] options:NSDataReadingUncached error:nil];


NSDictionary  *dictResponse = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:Nil];
NSLog(@"%@",dictResponse);
响应

{
  "status": "OK",
  "predictions": [ {
    "description": "Paris, France",
    "id" : "691b237b0322f28988f3ce03e321ff72a12167fd",
    "reference": "CiQYAAAA0Q_JA...kT3ufVLDDvTQsOwZ_tc",
    "terms": [ {
      "value": "Paris",
      "offset": 0
    }, {
      "value": "France",
      "offset": 7
    } ],
    "types": [ "geocode" ],
    "matched_substrings": [ {
      "offset": 0,
      "length": 5
    } ]
  }
根据
预测的对象
数组没有位置信息。您可以使用
reference
属性检索地点详细信息

返回的预测旨在呈现给用户,以帮助用户选择所需的位置。您可以发送“地点详细信息”请求,以获取有关返回的任何地点的更多信息

试试这个

NSString * urlString =[NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?address=Your address &sensor=true"];
NSURL * url=[NSURL URLWithString:urlString];
NSMutableURLRequest * request=[NSMutableURLRequest requestWithURL:url];
NSURLResponse * response;
NSError * error;
NSData * responseData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString * outputData=[[NSString alloc]initWithData:responseData encoding:NSASCIIStringEncoding];
NSLog(@" output is %@",outputData);

在此过程中,将“您的地址”更改为您想要的位置

尝试以下Swift代码:-

import UIKit
import GooglePlaces

class TableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {



@IBOutlet weak var txtField: UITextField!
@IBOutlet weak var tableView: UITableView!

var tableData = [GMSAutocompletePrediction]()

var fetcher: GMSAutocompleteFetcher?

override func viewDidLoad() {
    super.viewDidLoad()

    self.view.backgroundColor = UIColor.white
    self.edgesForExtendedLayout = []

    let nsBoundsCorner = CLLocationCoordinate2D(latitude: 20.5937, longitude: 78.9629)

    let bounds = GMSCoordinateBounds(coordinate: nsBoundsCorner, coordinate: nsBoundsCorner)

    let filter = GMSAutocompleteFilter()
    filter.type = .establishment

    fetcher  = GMSAutocompleteFetcher(bounds: bounds, filter: filter)
    fetcher?.delegate = self

    txtField?.addTarget(self, action: #selector(textFieldDidChange(textField:)),for: .editingChanged)

    tableView.delegate = self
    tableView.dataSource = self

    self.tableView.reloadData()


    // Do any additional setup after loading the view.
}

@objc func textFieldDidChange(textField: UITextField) {
        fetcher?.sourceTextHasChanged(txtField.text!)
    }
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return tableData.count
}

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    var section = indexPath.section
    var row = indexPath.row

    let cell1 : UITableViewCell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell1")

    cell1.selectionStyle = UITableViewCellSelectionStyle.none
    cell1.backgroundColor = UIColor.clear
    cell1.contentView.backgroundColor = UIColor.clear
    cell1.textLabel?.textAlignment = NSTextAlignment.left
    cell1.textLabel?.textColor = UIColor.black
    cell1.textLabel?.font = UIFont.systemFont(ofSize: 14.0)

    cell1.textLabel?.text = tableData[indexPath.row].attributedFullText.string
    return cell1
}

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    txtField.text = tableData[indexPath.row].attributedFullText.string
    getLatLongFromAutocompletePrediction(prediction:tableData[indexPath.row])
}

 func getLatLongFromAutocompletePrediction(prediction:GMSAutocompletePrediction){

    let placeClient = GMSPlacesClient()

    placeClient.lookUpPlaceID(prediction.placeID!) { (place, error) -> Void in
        if let error = error {
            //show error
            return
        }

        if let place = place {
            place.coordinate.longitude //longitude 
            place.coordinate.latitude //latitude
        } else {
        //show error
        }
    }
}
}

extension TableViewController: GMSAutocompleteFetcherDelegate {


func didAutocomplete(with predictions: [GMSAutocompletePrediction]) {

    tableData.removeAll()

    for prediction in predictions{

        tableData.append(prediction)

    }
    tableView.reloadData()
}

func didFailAutocompleteWithError(_ error: Error) {
    print(error.localizedDescription)
   }
}

你得到了什么回应?你的url看起来怎么样?@vokilam,谢谢你的回复。我发布了回复