我想使用Google API搜索特定城市的地点

时间:2017-05-14 09:45:40

标签: ios objective-c google-maps-sdk-ios

我想搜索UITextField中的地点,当我点击导航到其他视图控制器的GMSAutocompleteViewController时,我正在使用UITextField,但我希望搜索列表位于同一视图中我想要来自特定城市的地方,现在它可以在屏幕截图中显示来自世界各地的地方。这是我现在的代码:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    GMSAutocompleteViewController *acController = [[GMSAutocompleteViewController alloc] init];
    acController.delegate = self;

    [self presentViewController:acController animated:YES completion:nil];

    return true;
}


- (void)viewController:(GMSAutocompleteViewController *)viewController
didAutocompleteWithPlace:(GMSPlace *)place {
    // Do something with the selected place.
    NSLog(@"Place name %@", place.name);
    NSLog(@"Place address %@", place.formattedAddress);
    NSLog(@"Place attributions %@", place.attributions.string);
    NSLog(@"lat and log%f", place.coordinate.latitude);
    NSLog(@"lang %f", place.coordinate.longitude);

    _searchTextField.text = place.name;

    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)viewController:(GMSAutocompleteViewController *)viewController
didFailAutocompleteWithError:(NSError *)error {
    // TODO: handle the error.
    NSLog(@"error: %ld", (long)[error code]);
    [self dismissViewControllerAnimated:YES completion:nil];
}

// User canceled the operation.
- (void)wasCancelled:(GMSAutocompleteViewController *)viewController {
    NSLog(@"Autocomplete was cancelled.");
    [self dismissViewControllerAnimated:YES completion:nil];
}

Screenshot_1

Screenshot_2

3 个答案:

答案 0 :(得分:0)

GMSAutoCompleteViewController负责处理用户输入以及获取自动填充结果并显示它们。

要拥有自定义的ViewController,您需要自己完成上述所有过程。 按照此link以编程方式从Google API中获取自动填充结果。 https://developers.google.com/places/ios-api/autocomplete#get_place_predictions_programmatically

答案 1 :(得分:0)

限制特定区域搜索的最佳方法是创建路径并检查是否在其中请求lat,lng。此方法也可以用于自动完成API,一旦用户从行中选择了一些结果,我们就可以得到它的lat,lng并检查它们是否在我们的城市/国家路径中。 enter image description here 下面是一些更好理解的代码。例如,我希望用户限制在阿联酋。 首先获取阿联酋的边界坐标并将其保存在列表中。下面的代码可用于获取边界坐标。

func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D)  {

    self.view.endEditing(true)

    if(isDrawing == true)
    {
        self.viewMap.clear()
        path.add(coordinate)
        let polyline = GMSPolyline(path: path)
        polyline.strokeColor = .blue
        polyline.strokeWidth = 3.0
        polyline.map = self.viewMap
        points.add(String(format:"%f,%f",coordinate.latitude,coordinate.longitude))
    }

}

一旦你标记了整个边界点,那么可以像这样保存

let strPoints:String! = points.componentsJoined(by: "|")
print(strPoints)

您将在控制台中看到类似这样的内容

"26.050692,56.086607|26.117957,54.749640|25.901079,53.631803|24.620641,51.447101|24.274502,51.575709|22.926947,52.593388|22.777518,53.985706|22.645116,55.149715|22.701870,55.227998|23.016172,55.222406|23.375945,55.401338|23.698908,55.574679|23.842193,55.524355|23.949552,55.501988|24.046609,55.787162|24.061928,56.010827|24.184407,55.982869|24.220108,55.966095|24.204809,55.837486|24.255799,55.764795|24.403556,55.848670|24.540967,55.775979|24.637573,55.809528|24.759496,55.837486|24.916802,55.820711|24.967504,55.871037|24.987778,55.960503|24.967504,56.061152|24.876227,56.072336|24.886372,55.977278|24.754418,56.066744|24.769651,56.206535|24.835637,56.234493|24.932015,56.351918|24.972573,56.379876|25.256102,57.028506|25.896649,56.732150|26.368532,56.687416|26.488706,56.268043|26.348491,55.943728|26.388570,55.233589|26.413612,54.322151|26.183019,54.350110|26.107726,54.355701|26.027361,54.361293"

复制并保存在您的代码中

    path = GMSMutablePath()
    points = NSMutableArray()
    let pointsStr:String! = "26.050692,56.086607|26.117957,54.749640|25.901079,53.631803|24.620641,51.447101|24.274502,51.575709|22.926947,52.593388|22.777518,53.985706|22.645116,55.149715|22.701870,55.227998|23.016172,55.222406|23.375945,55.401338|23.698908,55.574679|23.842193,55.524355|23.949552,55.501988|24.046609,55.787162|24.061928,56.010827|24.184407,55.982869|24.220108,55.966095|24.204809,55.837486|24.255799,55.764795|24.403556,55.848670|24.540967,55.775979|24.637573,55.809528|24.759496,55.837486|24.916802,55.820711|24.967504,55.871037|24.987778,55.960503|24.967504,56.061152|24.876227,56.072336|24.886372,55.977278|24.754418,56.066744|24.769651,56.206535|24.835637,56.234493|24.932015,56.351918|24.972573,56.379876|25.256102,57.028506|25.896649,56.732150|26.368532,56.687416|26.488706,56.268043|26.348491,55.943728|26.388570,55.233589|26.413612,54.322151|26.183019,54.350110|26.107726,54.355701|26.027361,54.361293"
    let pp = pointsStr.components(separatedBy: "|")
    for item in pp {

        let coor = item.components(separatedBy: ",")
        let lat:Double! = (coor[0] as AnyObject).doubleValue
        let lng:Double! = (coor[1] as AnyObject).doubleValue
        let coordinate:CLLocationCoordinate2D! = CLLocationCoordinate2D(latitude: lat, longitude: lng)
        path.add(coordinate)
    }

    if(self.isCoordinateInside(coordinate: CLLocationCoordinate2D(latitude: selectedPlace.lat, longitude: selectedPlace.lng)) == true)
    {

    }



func isCoordinateInside(coordinate: CLLocationCoordinate2D)->Bool{

    if (GMSGeometryContainsLocation(coordinate, path, true) == true)
    {

        print("locationPoint is in polygonPath.")
        return true

    }
    else
    {

        print("locationPoint is NOT in polygonPath.")
        return false

    }
}

答案 2 :(得分:0)

首先转到"Google Places API"并获取您的API密钥。

然后,使用下面提到的代码来创建请求

 var url: String = 
 "https://maps.googleapis.com/maps/api/place/search/json?location=\
 (lat),\(lang)&radius=100&sensor=true&key=\(GOOGLE_API_KEY)"

 var googleRequestURL = URL(string: url)

 var request = NSMutableURLRequest(url: googleRequestURL!)

 request.httpMethod = "GET"

 request.setValue("application/json", forHTTPHeaderField: "Content-
 Type")

根据需要更改半径。请输入你的当前位置。