项目作者: pengrad

项目描述 :
Scale bar for Android Maps (Google Maps, OSM, MapBox, Yandex)
高级语言: Java
项目地址: git://github.com/pengrad/MapScaleView.git
创建时间: 2016-10-06T08:02:39Z
项目社区:https://github.com/pengrad/MapScaleView

开源协议:Apache License 2.0

下载


Map Scale View

Download
Android Arsenal

Scale view for any Android Maps SDK (not only Google Maps)

Image

Contributing

I encourage you to participate in this project. Feel free to open issues with bugs or ideas, fork and send pull requests.
Check list of “help wanted” issues to start with.

Usage

  1. dependencies {
  2. implementation 'com.github.pengrad:mapscaleview:1.6.0'
  3. }

Include in layout file over map

  1. <FrameLayout
  2. xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. android:id="@+id/activity_main"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent">
  7. <fragment
  8. android:id="@+id/mapFragment"
  9. class="com.google.android.gms.maps.SupportMapFragment"
  10. android:layout_width="match_parent"
  11. android:layout_height="match_parent"></fragment>
  12. <com.github.pengrad.mapscaleview.MapScaleView
  13. android:id="@+id/scaleView"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:layout_gravity="bottom|end"
  17. android:layout_margin="4dp"></com.github.pengrad.mapscaleview.MapScaleView>
  18. </FrameLayout>

With miles or custom style

  1. <com.github.pengrad.mapscaleview.MapScaleView
  2. android:id="@+id/scaleView"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content"
  5. android:layout_gravity="bottom|end"
  6. android:layout_margin="4dp"
  7. app:scale_maxWidth="100dp"
  8. app:scale_color="#009"
  9. app:scale_miles="true"
  10. app:scale_outline="true"
  11. app:scale_strokeWidth="3dp"
  12. app:scale_textSize="20sp"
  13. app:scale_expandRtl="true"></com.github.pengrad.mapscaleview.MapScaleView>

Update on map changed

  1. val scaleView: MapScaleView = findViewById(R.id.scaleView)
  2. val cameraPosition = map.cameraPosition
  3. // need to pass zoom and latitude
  4. scaleView.update(cameraPosition.zoom, cameraPosition.target.latitude)

Full example with subscribing to map events and updating scale view

  1. override fun onMapReady(googleMap: GoogleMap) {
  2. map = googleMap
  3. googleMap.setOnCameraMoveListener(this)
  4. googleMap.setOnCameraIdleListener(this)
  5. }
  6. override fun onCameraMove() {
  7. val cameraPosition = map.cameraPosition
  8. scaleView.update(cameraPosition.zoom, cameraPosition.target.latitude)
  9. }
  10. override fun onCameraIdle() {
  11. val cameraPosition = map.cameraPosition
  12. scaleView.update(cameraPosition.zoom, cameraPosition.target.latitude)
  13. }

Refer to the sample project on how to use scale view with other Android Maps SDK (Mapbox).

Customization

  1. mapScaleView.setColor(@ColorInt int color)
  2. mapScaleView.setTextSize(float textSize)
  3. mapScaleView.setStrokeWidth(float strokeWidth)
  4. mapScaleView.setTextFont(Typeface font)
  5. // enable/disable white outline, enabled by default
  6. mapScaleView.setOutlineEnabled(false)
  7. mapScaleView.metersAndMiles() // default
  8. mapScaleView.metersOnly()
  9. mapScaleView.milesOnly()
  10. // expand scale bar from right to left, disabled by default
  11. mapScaleView.setExpandRtlEnabled(true)