LoginSignup
2
3

More than 5 years have passed since last update.

Xamarin.iOS で Google Maps SDK の地図のキャプチャを取る方法

Last updated at Posted at 2017-02-20

iOSネイティブの場合

iOS アプリ開発で Google Maps を使用するには、

を使用します。

で、アプリ内で表示している地図の画像をキャプチャするには、以下のようなコードを書くようです。

UIGraphicsBeginImageContext(mapView_.frame.size);
[mapView_.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *screenShotImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

via Google Maps IOS SDK 1.2 need snapshot of map view - Stack Overflow

さらに、Retinaディスプレイにも対応させたい場合、

で示されているように、UIGraphicsBeginImageContextWithOptions(ICON_SIZE, NO, 0); とするようです。

Xamarin.iOS の場合

以上のようなiOSネイティブの知識を踏まえて、これを Xamarin.iOS でやってみると、以下のようになります。

UIGraphics.BeginImageContextWithOptions(mapView.Frame.Size, false, 0f);
NativeMap.Layer.RenderInContext(UIGraphics.GetCurrentContext());
UIImage snapshot = UIGraphics.GetImageFromCurrentImageContext();
UIGraphics.EndImageContext();

変数 snapshot がキャプチャされた画像(UIImage)です。

Xamarin.iOS で UIImage を任意の画像形式の Stream に変換

この画像を PNG で保存したいと思います。

Stream pngStream = snapshot.AsPNG().AsStream(); // .AsJPEG() もあるよ

これだけです、あまりに簡単すぎて笑ってしまいました。
あとは Stream をディスクなどに書き込むだけです。

こんな感じで、地図のスナップショットを撮ることができます(下が地図で上がスナップショット)。

おまけ: Google Maps Android API の場合

Android アプリで Google Maps を使ってスナップショットを撮るには、

を使います。コールバックで Bitmap が返ってくるので、こちらの方が簡単です。

両者を組み込んで Xamarin.Forms.GoogleMaps で スナップショットが撮れる機能 をそのうち追加します。

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3