2

このコードを使用して、iOS 用の Google マップにマーカーを作成します。

self.mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
    self.mapView.myLocationEnabled = YES;
    self.mapView.accessibilityElementsHidden = NO;
    self.mapView.frame = self.view.bounds;
    self.mapView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    self.mapView.delegate = self;
    self.mapView.settings.myLocationButton = YES;
    [self.view addSubview:self.mapView];

GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(-33.86, 151.20);
marker.map = self.mapView;

しかし、マーカーを次のようにする必要があります。

ここに画像の説明を入力

Apple Map でそれを行う方法は知っていますが、Google Map を使用する必要があります (一部の都市では Apple Map ではほとんど何も表示されないため)。

Googleマップの場合、次のようなコードのみが見つかりました:

marker.icon = image; // image from xib file

そのため、マーカーごとに xib から画像を作成する必要があります。マーカーがたくさんあるので、このアプローチではメモリ警告が表示される可能性があります。

そのようなマーカーを実装するより良い方法を知っている人はいますか?

4

4 に答える 4

9

GMSMarker では UIImage をアイコンとして設定することしかできないため、次のようなものを実装しました (Swift にありますが、Objective C でも概念は同じです)。

var gasView = UIView(frame:CGRectMake(0, 0, 30, 37))
//Add Label
var lblPrecio = UILabel(frame: CGRectMake(0, 37-18, 30, 10))
lblPrecio.text = "hello"
lblPrecio.textAlignment = NSTextAlignment.Center
gasView.addSubview(lblPrecio)
//Add Logo
var logo = UIImageView(frame: CGRectMake(30-23, 2, 16, 16))
logo.image = UIImage(named: "Logo")
gasView.addSubview(logo)
//Add Callout Background Image
gasView.backgroundColor = UIColor(patternImage: UIImage(named: "Callout")!)

marker.icon = imageFromView(gasView)

関数 imageFromView は次のとおりです。

private func imageFromView(aView:UIView) -> UIImage {
    if(UIScreen.mainScreen().respondsToSelector("scale")) {
        UIGraphicsBeginImageContextWithOptions(aView.frame.size, false, UIScreen.mainScreen().scale)
    }
    else {
        UIGraphicsBeginImageContext(aView.frame.size)
    }
    aView.layer.renderInContext(UIGraphicsGetCurrentContext())
    var image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return image
}

お役に立てれば。

于 2014-12-29T19:52:50.610 に答える
0
 Set the color to your marker first and then camera position to that coordinate, So that whenever your app will start it redirect to that coordinate

 marker.icon = [UIColor redColor];
 GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] initWithPath:< CLLocationCoordinate2DMake>];
        [self.mapView animateWithCameraUpdate:[GMSCameraUpdate fitBounds:bounds withPadding:10.0f]];
        bounds=nil;
    GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] initWithPath:path];
            [self.mapView animateWithCameraUpdate:[GMSCameraUpdate fitBounds:bounds withPadding:0.0f]];
            bounds=nil;
于 2015-07-17T07:37:04.767 に答える
0

私の知る限り、それが唯一の方法です。ただし、メモリまたはパフォーマンスの問題を防ぐために Google マップのマーカーを制限する方法については、Google のベスト プラクティスに従うことができます

于 2014-05-08T07:59:53.260 に答える
0

この例に従ってください。必要なものを実装するのは非常に簡単です。

  • uiimageview を宣言する
  • 2 番目のイメージビューを宣言します (この場合は b&w の女の子が表示されます)。
  • したがって、サブビューとして 2 番目の画像を最初の画像に追加します。
  • これで、最初の画像を marker.icon = (HERE THE FIRST IMAGE!!); に追加できます。

これがあなたを助けることを願っています。

于 2014-06-12T11:12:41.123 に答える