GoogleMapの任意の位置にマーカーを立てる

地図を表示させるぞ

…と思ってもそもそやっていたのだけれど、途中のコード残しとくの忘れた。とりあえず最終的なやつ。

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }


    /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user will be prompted to install
     * it inside the SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        LatLng fuji = new LatLng(35.362859,138.730883);

        BitmapDescriptor iconSanta = BitmapDescriptorFactory.fromResource(R.drawable.christmas_santa_sori_s);
        MarkerOptions option = new MarkerOptions();
        option.icon(iconSanta).position(fuji);
        mMap.addMarker(option);
        mMap.moveCamera(CameraUpdateFactory.newLatLng(fuji));
    }
}

めちゃめちゃ簡単なんだなぁ。ついでにマーカーの画像も変えてみた。

参考

https://developers.google.com/maps/documentation/android-sdk/start

https://developers.google.com/android/reference/com/google/android/gms/maps/model/MarkerOptions

https://seesaawiki.jp/w/moonlight_aska/d/%A5%DE%A1%BC%A5%AB%A1%BC%A4%F2%A5%AB%A5%B9%A5%BF%A5%DE%A5%A4%A5%BA%A4%B9%A4%EB

https://www.irasutoya.com/2012/10/blog-post_24.html

カテゴリー: のーと タグ: , , , , , パーマリンク

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です