Mapbox Android入门(一)

0.166字数 619阅读 2812

写在前面:

公司项目都是跟地图有关的,国内地图sdk接触过高德地图和百度地图,都是中文文档,大家有兴趣可去自己查看,国外地图sdk做的好的有arcgis for android和mapbox,本文将会对mapbox进行入门教程。


android sdk 官网:https://www.mapbox.com/android-docs/map-sdk/overview/

github地址:https://github.com/mapbox/mapbox-gl-native/tree/master/platform/android

官方demo github地址:https://github.com/mapbox/mapbox-android-demo


配置:

access_token:可以去官网注册账号并申请(ps:为方便体验,下面提供一条token)

pk.eyJ1IjoiY2FtbWFjZSIsImEiOiJjaW9vbGtydnQwMDAwdmRrcWlpdDVoM3pjIn0.Oy_gHelWnV12kJxHQWV7XQ

添加依赖:

dependencies {

  implementation 'com.mapbox.mapboxsdk:mapbox-android-sdk:5.5.2'

}

在application中初始化

class MyApp :Application(){

    override fun onCreate() {

        super.onCreate()

        Mapbox.getInstance(this, getString(R.string.access_token))

    }

}

在AndroidManifest添加权限

< uses-permission android:name="android.permission.INTERNET" />

< uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

加载地图:

xml:

<com.mapbox.mapboxsdk.maps.MapView

        android:id="@+id/mapView"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        mapbox:mapbox_cameraTargetLat="31.855500"

        mapbox:mapbox_cameraTargetLng="117.204593"

        mapbox:mapbox_cameraZoom="11"

        mapbox:mapbox_styleUrl="@string/mapbox_style_mapbox_streets"

        mapbox:mapbox_uiLogo="false"

        mapbox:mapbox_uiAttribution="false"/>

xml中mapview设置属性:https://github.com/mapbox/mapbox-gl-native/blob/master/platform/android/MapboxGLAndroidSDK/src/main/res/values/attrs.xml

MapViewActivity:

onCreate();onStart();onResume();onPause();onStop();onSaveInstanceState();onLowMemory();onDestroy();部分生命周期调用建议放在baseActivity中

class MapViewActivity :AppCompatActivity(){

    private lateinit var mapBox:MapboxMap

    override fun onCreate(savedInstanceState:Bundle?) {

        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_simple_mapview)

        mapView.onCreate(savedInstanceState)

        setSupportActionBar(toolBar)

        supportActionBar?.title="加载地图"

        mapView.getMapAsync {

            mapBox=it

            //可在xml中设置,也可代码动态设置

            //it.animateCamera(CameraUpdateFactory.newLatLng(LatLng(31.855500,117.204593)),1000 )

        }

    }

    override fun onStart() {

        super.onStart()

        mapView.onStart()

    }

    override fun onResume() {

        super.onResume()

        mapView.onResume()

    }

    override fun onPause() {

        super.onPause()

        mapView.onPause()

    }

    override fun onStop() {

        super.onStop()

        mapView.onStop()

    }

    override fun onDestroy() {

        super.onDestroy()

        mapView.onDestroy()

    }

    override fun onLowMemory() {

        super.onLowMemory()

        mapView.onLowMemory()

    }

    override fun onSaveInstanceState(outState:Bundle?) {

        super.onSaveInstanceState(outState)

        outState?.let { mapView.onSaveInstanceState(it) }

    }

    override fun onCreateOptionsMenu(menu:Menu?):Boolean {

        menuInflater.inflate(R.menu.menu_map_style, menu)

        return super.onCreateOptionsMenu(menu)

    }

    override fun onOptionsItemSelected(item:MenuItem?):Boolean {

        //通过菜单,选择不同的图层

        when(item?.itemId){

            R.id.menu_streets->mapBox.setStyleUrl(Style.MAPBOX_STREETS)

            R.id.menu_dark->mapBox.setStyleUrl(Style.DARK)

            R.id.menu_light->mapBox.setStyleUrl(Style.LIGHT)

            R.id.menu_outdoors->mapBox.setStyleUrl(Style.OUTDOORS)

            R.id.menu_satelite->mapBox.setStyleUrl(Style.SATELLITE)

            R.id.menu_satelite_streets->mapBox.setStyleUrl(Style.SATELLITE_STREETS)

        }

        return super.onOptionsItemSelected(item)

    }

}


成功加载

        mapbox入门教程就完成了,大家通过上面的代码自己复制就可以成功加载地图了,还有点、线、面,图层、geojson的加载等知识会在之后介绍。

推荐阅读更多精彩内容