【问题标题】:How to get lat and long of center google map iOS如何获取中心谷歌地图 iOS 的经纬度
【发布时间】:2014-11-21 19:16:04
【问题描述】:

我正在使用故事板构建一个 iOS 应用程序。我在我的应用程序中集成了谷歌地图并在点击时获取位置地址。

我想在地图中心实现一个 pin 滚动地图 pin 将移动并提供与 Uber 或 Ola Cabs 应用程序中相同的位置地址。

有人可以帮助我如何实现这一点,我找到了一个教程(http://www.raywenderlich.com/81103/introduction-google-maps-ios-sdk-swift),但它很快,但我必须在目标 c 中实现。

这是我的代码:

- (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate{

    // coordinate contains your coordinate :)

    NSLog(@"did tap at coordinate: (%f, %f)", coordinate.latitude, coordinate.longitude);

         NSString *address=[NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&key=AIzaSjhsdfg67tahdsg8981k8_5aJoZps",coordinate.latitude, coordinate.longitude];

    NSURL *url=[NSURL URLWithString:address];

    NSData* data = [NSData dataWithContentsOfURL:url];

    if ((unsigned long)data.length > 3) {

        // NSString *myString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

        NSString *file=@"/Data.json";

        [data writeToFile:file atomically:YES];

        NSArray *ys_avatars = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

        NSLog(@"array=%@",ys_avatars);

            NSLog(@"status====%@",[ys_avatars valueForKey:@"status"]);

             if([[ys_avatars valueForKey:@"status"]  isEqual: @"OK"]){

        NSArray *results=[ys_avatars valueForKey:@"results"];

            NSLog(@"formatted_address====%@",[results valueForKey:@"formatted_address"] );

            NSArray *address=[results valueForKey:@"formatted_address"];

            NSLog(@"add=%@",address[0]);

               _myAddress.text=address[0];

        }

  }

}

【问题讨论】:

  • 如果我没听错的话,你总是想得到地图中心的纬度和经度吗?这就是 UBER 的工作方式,如果是这种情况,我认为您不需要实施 didTapAtCoordinate 而是应该使用 - mapView:didChangeCameraPosition:
  • @VigThankYeaaa 你是绝对正确的,你能不能给我一个代码示例我如何实现这个 - 你能告诉我如何在相机位置的中心经纬度,我是对的吗是我们实现这一点所需要的——

标签: ios objective-c google-maps


【解决方案1】:

我无法回答您的问题,您可以通过这种方式获得地图中心的经纬度:

    - (void)mapView:(GMSMapView *)mapView didChangeCameraPosition:(GMSCameraPosition *)position
{
       double latitude = mapView.camera.target.latitude;
       double longitude = mapView.camera.target.longitude;

       CLLocationCoordinate2D center = CLLocationCoordinate2DMake(latitude, longitude);


        marker.position = center;

 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-11
    相关资源
    最近更新 更多