3

我正在尝试使用应用程序进行地理Google Map围栏iPhone。可以找到很多关于 MKMapView 的教程。但找不到 GMSMapView。基本的事情是如何将屏幕坐标(x,y)转换为 MapCoordinate lat/lng。是否有任何API可用于该转换的谷歌地图iOS?谢谢

4

2 回答 2

2

你可以使用这样的东西:

GMSMapView* mapView = ...;
CGPoint point = ...;
...
CLLocationCoordinate2D coordinate = 
    [mapView.projection coordinateForPoint: point];

更新:

对该projection物业的评论GMSMapView.h如下:

/**
 * The GMSProjection currently used by this GMSMapView. This is a snapshot of
 * the current projection, and will not automatically update when the camera
 * moves. The projection may be nil while the render is not running (if the map
 * is not yet part of your UI, or is part of a hidden UIViewController, or you
 * have called stopRendering).
 */
@property (nonatomic, readonly) GMSProjection *projection;

因此,您只能.projection在地图渲染后访问该属性。loadView如果您尝试在或期间访问它,它将为零viewDidLoad

我不知道是否有更好的方法来判断地图是否已渲染,但我注意到该mapView:didChangeCameraPosition:方法在地图视图首次显示后被调用一次,并且地图的projection属性在那里有效。

因此,在您的视图控制器的标题中,添加GMSMapViewDelegate

@interface ViewController : UIViewController <GMSMapViewDelegate>

分配地图视图时,分配委托:

_map = [GMSMapView mapWithFrame: CGRectMake(0, 0, width, height) camera: camera]; 
_map.delegate = self;
[self.view addSubview: _map];

然后添加委托方法:

- (void)mapView: (GMSMapView*)mapView
    didChangeCameraPosition: (GMSCameraPosition*)position
{
    CGPoint point = CGPointMake(x, y); 
    CLLocationCoordinate2D coordinate = 
        [_map.projection coordinateForPoint: point];
}

请注意,mapView:didChangeCameraPosition:每次用户更改相机时都会调用它,因此您可能需要使用一个标志,以便您只在第一次mapView:didChangeCameraPosition:调用时进行计算。

于 2013-03-02T03:58:06.087 回答
0

No need to convert x,y to lat,long.

GMSCircle *fence = [GMSCircle circleWithPosition:locationCord radius:fenceRadius];
    [fence setFillColor:[UIColor colorWithRed:102.0/255 green:178.0/255 blue:255.0/255 alpha:0.3]];
    [fence setZIndex:100];

    [fence setMap: _map];

在制作 GMSMapView 时添加此代码,地理围栏将与您的位置标记一起显示。

于 2013-11-19T12:57:02.737 回答