Mapkit の代わりに Google Maps SDK for iOS を使用することを考えていたマップに基づいて iOS アプリケーションを作成しました。ドキュメントは見つかりましたが、カスタム アノテーション ビューに関連するメソッドが見つかりませんでした。カスタム注釈ビュー (情報ウィンドウ) を作成し、コンテンツ (タイトル、スニペット) を追加する方法。
2 に答える
皆さんのことはわかりませんが、Google のレンダリングされた UIView 情報ウィンドウが少し制限されていることがわかりました。SMCalloutViewとRyan Maxwell のサンプル プロジェクトを使用すると、よりインタラクティブなビューを表示できます。
これは、2014 年 6 月 10 日現在、Google Maps SDK v1.8.1 で動作します。
まず、セットアップを行います。
#import <SMCalloutView/SMCalloutView.h>
static const CGFloat CalloutYOffset = 10.0f;
@interface ViewController ()
@property (strong, nonatomic) SMCalloutView *calloutView;
@property (strong, nonatomic) UIView *emptyCalloutView;
@end
を初期化SMCalloutView
し、ボタンを追加してから、空の を作成しますUIView
。
- (void)viewDidLoad
{
/* all your other view init, settings, etc... */
self.calloutView = [[SMCalloutView alloc] init];
self.calloutView.hidden = YES;
UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[button addTarget:self
action:@selector(calloutAccessoryButtonTapped:)
forControlEvents:UIControlEventTouchUpInside];
self.calloutView.rightAccessoryView = button;
self.emptyCalloutView = [[UIView alloc] initWithFrame:CGRectZero];
}
Maps SDK を満たすために空を描画する必要UIView
がありますが、表示するビューはSMCalloutView
. また、吹き出しビューに再利用可能な垂直オフセットを設定しました。
情報ウィンドウの呼び出しを処理するデリゲート メソッドを追加します。
#pragma mark - GMSMapViewDelegate
- (UIView *)mapView:(GMSMapView *)mapView markerInfoWindow:(GMSMarker *)marker {
CLLocationCoordinate2D anchor = marker.position;
CGPoint point = [mapView.projection pointForCoordinate:anchor];
self.calloutView.title = marker.title;
self.calloutView.calloutOffset = CGPointMake(0, -CalloutYOffset);
self.calloutView.hidden = NO;
CGRect calloutRect = CGRectZero;
calloutRect.origin = point;
calloutRect.size = CGSizeZero;
[self.calloutView presentCalloutFromRect:calloutRect
inView:mapView
constrainedToView:mapView
animated:YES];
return self.emptyCalloutView;
}
- (void)mapView:(GMSMapView *)pMapView didChangeCameraPosition:(GMSCameraPosition *)position {
/* move callout with map drag */
if (pMapView.selectedMarker != nil && !self.calloutView.hidden) {
CLLocationCoordinate2D anchor = [pMapView.selectedMarker position];
CGPoint arrowPt = self.calloutView.backgroundView.arrowPoint;
CGPoint pt = [pMapView.projection pointForCoordinate:anchor];
pt.x -= arrowPt.x;
pt.y -= arrowPt.y + CalloutYOffset;
self.calloutView.frame = (CGRect) {.origin = pt, .size = self.calloutView.frame.size };
} else {
self.calloutView.hidden = YES;
}
}
- (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate {
self.calloutView.hidden = YES;
}
- (BOOL)mapView:(GMSMapView *)mapView didTapMarker:(GMSMarker *)marker {
/* don't move map camera to center marker on tap */
mapView.selectedMarker = marker;
return YES;
}
ここでは、マーカー タイトルとスニペットを含むアラート ビューを使用して、コールアウト ボタンへのタッチを処理します。
- (void)calloutAccessoryButtonTapped:(id)sender {
if (mapView_.selectedMarker) {
GMSMarker *marker = mapView_.selectedMarker;
//NSDictionary *userData = marker.userData;
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:marker.title
message:marker.snippet
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
}
}
明らかに、ViewController(.h) が GMSMapViewDelegate をリッスンしていることを確認してください。
@interface ViewController : UIViewController <GMSMapViewDelegate>
そして、それは基本的にうまくいくはずです。完全な xcode プロジェクトについては、前述のRyan Maxwellの例を参照してください。
GoogleMaps.Framework 内の GMSMapView.h を確認すると、標準のタイトルとスニペットだけを使用する代わりに、マーカーのカスタム情報ウィンドウを追加できる以下のメソッドが表示されます。
あなたは(op) annotationView =infoWindowと言うことに注意してください。
/**
* Called when a marker is about to become selected, and provides an optional
* custom info window to use for that marker if this method returns a UIView.
* If you change this view after this method is called, those changes will not
* necessarily be reflected in the rendered version.
*
* The returned UIView must not have bounds greater than 500 points on either
* dimension. As there is only one info window shown at any time, the returned
* view may be reused between other info windows.
*
* @return The custom info window for the specified marker, or nil for default
*/
- (UIView *)mapView:(GMSMapView *)mapView
markerInfoWindow:(id<GMSMarker>)marker;