0
0

MapBoxMapをAndroid、JAVAのみで使ってマーカーで詰まった話

Posted at

MapBoxMapは便利な地図APIです。

↑MapBoxMapの概要はこれ見てください
GoogleMap APIより体感お手軽に使えるしなんかマップの見た目とかかなりカスタマイズ効くので個人的には特に理由がなければ埋め込みのMapはこれを使うのが良いと思う。

記事の内容

MapBoxMapのドキュメントはほぼKotlinで書かれてる。
Kotlinはとても強い言語なのでやはり特に理由がなければJavaよりこちらを使うべきだが、なんかの理由でJavaでAndroidアプリを作らなければならない場合があった場合、公式ドキュメントに書かれているメソッドとかクラスとかが一部うまく動かないので、主に詰まったところをこの記事に書いていく。

マーカーを打つ

公式ドキュメント:

ここにはmapboxmapでマーカーを打つ方法が書かれていて、以下のようなコードでマーカーを追加できるとある。

// Create an instance of the Annotation API and get the PointAnnotationManager.
val annotationApi = mapView?.annotations
val pointAnnotationManager = annotationApi?.createPointAnnotationManager(mapView)
// Set options for the resulting symbol layer.
val pointAnnotationOptions: PointAnnotationOptions = PointAnnotationOptions()
    // Define a geographic coordinate.
    .withPoint(Point.fromLngLat(18.06, 59.31))
    // Specify the bitmap you assigned to the point annotation
    // The bitmap will be added to map style automatically.
    .withIconImage(YOUR_ICON_BITMAP)
// Add the resulting pointAnnotation to the map.
pointAnnotationManager?.create(pointAnnotationOptions)

このうち上部のAnnotaionAPIを取得する部分について

val annotationApi = mapView?.annotations
val pointAnnotationManager = annotationApi?.createPointAnnotationManager(mapView)

の部分がjavaだと以下のようになります

AnnotationPlugin annotationPlugin = AnnotationPluginImplKt.getAnnotations(mapView);
PointAnnotationManager pointAnnotationManager = PointAnnotationManagerKt.createPointAnnotationManager(annotationPlugin, new AnnotationConfig());

Kotlinにおけるクラスのプロパティはget,setと名前につくメソッドとしてjavaに変換される仕様ですが、どうもannotationsのプロパティに対応するgetAnnotationsが使えない様子です。MapBoxmapをjavaで使用するときはこのようにそのままだと取得できないプロパティがいくつかあるため、代わりにAnnotationPluginImplのようなクラスを使用します(KtはKotlinにおいてトップレベルに置かれた関数がjavaに変換される際に付与される文字です)。

短いけど以上

0
0
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
0
0