讲解Google map类基本用法和使用场景
map model 地图模型
学习google map,我们要知道其思想是通过建立一些模型和实体概念来简化相关认识。
BitmapDescriptor | 定义位图图像。 |
BitmapDescriptorFactory | 用于创建位图图像的定义,用于标记图标和地面叠加。 |
ButtCap | 在Polyline 实线笔划图案的开始或结束顶点处准确平方的顶点,相当于在开始或结束顶点之外没有额外的顶点。 |
CameraPosition | 一个不可变类,它聚合所有摄像机位置参数,例如位置,缩放级别,倾斜角度和方位。 |
CameraPosition.Builder | 建立相机位置。 |
Cap | 不可变的上限可以应用于a的起点或终点Polyline 。 |
Circle | 地球表面上的一个圆圈(球冠)。 |
CircleOptions | 定义a的选项Circle 。 |
CustomCap | 位图叠加以a的起始或结束顶点为中心Polyline ,根据线的第一个或最后一个边的方向定向,并相对于线的笔划宽度进行缩放。 |
Dash | 表示为一个行程图案中使用短划线不可变类Polyline 或一的轮廓Polygon 或Circle 。 |
Dot | 表示为一个行程图案中的点的不可变类Polyline 或一的轮廓Polygon 或Circle 。 |
Gap | 表示用于在行程模式中使用的间隙的不可变类Polyline 或一的轮廓Polygon 或Circle 。 |
GroundOverlay | 地面叠加层是固定在地图上的图像。 |
的GroundOverlayOptions | 定义地面叠加层的选项。 |
IndoorBuilding | 代表一座建筑。 |
IndoorLevel | 表示建筑物中的水平。 |
JointType | 联合类型Polyline 和概述Polygon 。 |
LatLng | 表示一对纬度和经度坐标的不可变类,以度为单位存储。 |
的LatLngBounds | 表示纬度/经度对齐矩形的不可变类。 |
LatLngBounds.Builder | 这是一个能够根据一组LatLng点创建最小边界的构建器。 |
MapStyleOptions | 为a定义样式选项GoogleMap 。 |
Marker | 放置在地图表面特定点的图标。 |
的MarkerOptions | 为标记定义MarkerOptions。 |
PatternItem | 在一个行程模式使用一成不变的项目 Polyline 或轮廓Polygon 或Circle 。 |
PointOfInterest | 包含有关单击的PointOfInterest的信息。 |
Polygon | 地球表面的多边形。 |
的PolygonOptions | 定义多边形的选项。 |
Polyline | 折线是一个点列表,其中线段在连续点之间绘制。 |
的PolylineOptions | 定义折线的选项。 |
RoundCap | 半径等于行程宽度一半的半圆形帽,Polyline 以实线笔划图案的起点或终点为中心。 |
SquareCap | 在将笔画宽度的一半延伸到Polyline 具有实线笔划图案的开始或结束顶点之后,将其平方。 |
StreetViewPanoramaCamera | 一个聚合所有摄像机位置参数的不可变类。 |
StreetViewPanoramaCamera.Builder | 构建全景相机。 |
StreetViewPanoramaLink | 一个不可变类,表示指向另一个街景全景图的链接。 |
StreetViewPanoramaLocation | 一个不可变类,包含用户当前街景全景图的详细信息 |
StreetViewPanoramaOrientation | 一个聚合所有用户视点参数的不可变类。 |
StreetViewPanoramaOrientation.Builder | 构建街景全景方向。 |
StreetViewSource | 用于将街景搜索限制为选定来源的标识符。 |
Tile | 包含有关由a返回的Tile的信息TileProvider 。 |
TileOverlay | 平铺叠加是一组图像,显示在基本地图图块的顶部。 |
TileOverlayOptions | 定义TileOverlay的选项。 |
UrlTileProvider | 部分实现TileProvider 只需要指向要提供的图像的URL。 |
VisibleRegion | 包含定义在地图相机中可见的四边形多边形的四个点。 |
mapView 上的一些UI 就是对应上面的各种model,那我们了解这些模型相关的api,就能大致知道如何去运用地图了。比如如何添加相关model,及相关事件处理;
基本用法
- 画线:
private static final Dash DASH = new Dash(50); private static final Gap GAP = new Gap(20); private static final List<PatternItem> PATTERN_DASHED = Arrays.asList(DASH, GAP); mPolyline = mMap.addPolyline(new PolylineOptions() .add(latLng, new LatLng(latLng.latitude, toRadiusLatLng(latLng, 200).longitude)) .color(getResources().getColor(R.color.colorAccent)) .width(10) //短线 50 点为DOT 没有长度单位 .pattern(PATTERN_DASHED) .zIndex(2) .clickable(true));
- 标记marker
mCenterMarker = mMap.addMarker(new MarkerOptions() .position(latLng) //系统标记图标 .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)) .draggable(true) .zIndex(2) .title("detection marker")); //也可以换成自己的Bitmap .icon(BitmapDescriptorFactory.fromBitmap(getBitmap(xx))
-
多边形
mMutablePolygon = map.addPolygon(new PolygonOptions() .addAll(createRectangle(CENTER, 5, 5)) .addHole(createRectangle(new LatLng(-22, 128), 1, 1)) .addHole(createRectangle(new LatLng(-18, 133), 0.5, 1.5)) .fillColor(fillColorArgb) .strokeColor(strokeColorArgb) .strokeWidth(mStrokeWidthBar.getProgress()) .clickable(mClickabilityCheckbox.isChecked())); 就是多个线连接起来的
圆形
mCircle = mMap.addCircle(new CircleOptions()
.center(new LatLng(latLng.latitude , latLng.longitude))
.radius(200)
.strokeWidth(5)
.strokeColor(getResources().getColor(R.color.detectionCircle))
//填充色为透明
.fillColor(0x220000FF)
.zIndex(2)
.clickable(true));
UI Settings 设置
文档说明;该类只要控制地图界面上的UI 控件和一些手势动作,这里对一些重要的类做一些解析:
boolean | isCompassEnabled() Gets whether the compass is enabled/disabled. |
boolean | isIndoorLevelPickerEnabled() Gets whether the indoor level picker is enabled/disabled. |
boolean | isMapToolbarEnabled() Gets whether the Map Toolbar is enabled/disabled. |
boolean | isMyLocationButtonEnabled() Gets whether the my-location button is enabled/disabled. |
boolean | isRotateGesturesEnabled() Gets whether rotate gestures are enabled/disabled. |
boolean | isScrollGesturesEnabled() Gets whether scroll gestures are enabled/disabled. |
boolean | isTiltGesturesEnabled() Gets whether tilt gestures are enabled/disabled. |
boolean | isZoomControlsEnabled() Gets whether the zoom controls are enabled/disabled. |
boolean | isZoomGesturesEnabled() Gets whether zoom gestures are enabled/disabled. |
void | setAllGesturesEnabled(boolean enabled) Sets the preference for whether all gestures should be enabled or disabled. |
void | setCompassEnabled(boolean enabled) Enables or disables the compass. |
void | setIndoorLevelPickerEnabled(boolean enabled) Sets whether the indoor level picker is enabled when indoor mode is enabled. |
void | setMapToolbarEnabled(boolean enabled) Sets the preference for whether the Map Toolbar should be enabled or disabled. |
void | setMyLocationButtonEnabled(boolean enabled) Enables or disables the my-location button. |
void | setRotateGesturesEnabled(boolean enabled) Sets the preference for whether rotate gestures should be enabled or disabled. |
void | setScrollGesturesEnabled(boolean enabled) Sets the preference for whether scroll gestures should be enabled or disabled. |
void | setTiltGesturesEnabled(boolean enabled) Sets the preference for whether tilt gestures should be enabled or disabled. |
void | setZoomControlsEnabled(boolean enabled) Enables or disables the zoom controls. |
void | setZoomGesturesEnabled(boolean enabled) Sets the preference for whether zoom gestures should be enabled or disabled. |
一般的,我们是在地图加载成功后,进行map的UI Setting 设置,如:
@SuppressLint("MissingPermission")
private void initMapSetting() {
mMap.getUiSettings().setAllGesturesEnabled(true);
//mMap.getUiSettings().setZoomControlsEnabled(true);
mMap.getUiSettings().setCompassEnabled(false);
mMap.getUiSettings().setMyLocationButtonEnabled(true);
//禁止右下角的两个系统图标
mMap.getUiSettings().setMapToolbarEnabled(false);
//定位功能开启
mMap.setMyLocationEnabled(true);
//定位按钮点击事件
mMap.setOnMyLocationButtonClickListener(this);
mMap.setOnCameraChangeListener(this);
}
@SuppressLint("MissingPermission")
private void forbidMapOperation() {
mMap.getUiSettings().setAllGesturesEnabled(false);
//mMap.getUiSettings().setZoomControlsEnabled(false);
mMap.getUiSettings().setMyLocationButtonEnabled(false);
mMap.setMyLocationEnabled(false);
mMap.setOnMyLocationButtonClickListener(null);
mMap.setOnCameraChangeListener(this);
}
比如说 展现罗盘,缩放按钮,定位按钮,手势操作等设置。
回调事件
文档说明;主要常用的有 On(Map,Marker,Circle,MyLocationButton--定位按钮,Polygon--多边形,Polyline--线,InfoWindow--字体信息)ClickListener ,OnMapLongClickListener ,OnMarkerDragListener,onCameraChangeListener(相机位置和属性改变后);这些回调的规律是:围绕一些地图上的模型点击,移动,长按的事件进行回调;
缩放和移动的 文档说明 ;google地图的缩放级别从1(洲)——20(街道);注意缩放其实是相机的视角变化,并不是地图本身进行缩放,这一点对于移动也是,移动本身不是mapView的移动,而是地图上方的相机进行移动,这一点是地图和相机的关系,理解这一点很重要。
更改缩放级别并设置最小/最大缩放
CameraUpdateFactory.zoomIn()
并 CameraUpdateFactory.zoomOut()
给你一个CameraUpdate
改变缩放级别1.0,同时保持所有其他属性相同。
CameraUpdateFactory.zoomTo(float)
为您提供一个CameraUpdate
将缩放级别更改为给定值,同时保持所有其他属性相同的值。
CameraUpdateFactory.zoomBy(float)
并 CameraUpdateFactory.zoomBy(float, Point)
给你一个CameraUpdate
增加(或减少,如果值为负)的缩放级别给定的值。后者固定屏幕上的给定点,使得它保持在相同的位置(纬度/经度),因此它可以改变相机的位置以实现此目的。
您可能会发现设置首选的最小和/或最大缩放级别很有用。例如,如果您的应用显示感兴趣点周围的已定义区域,或者您使用的是具有有限缩放级别的自定义图块叠加层,则此功能可用于控制用户的体验。
设置缩放范围:
mMap.setMinZoomPreference(6.0f);
mMap.setMaxZoomPreference(14.0f);
改变相机位置
常见的位置变化有两种方便的方法。 CameraUpdateFactory.newLatLng(LatLng)
给你一个CameraUpdate
改变相机的经度和经度,同时保留所有其他属性。 CameraUpdateFactory.newLatLngZoom(LatLng, float)
为您提供CameraUpdate
更改相机的纬度,经度和变焦,同时保留所有其他属性。
为了在更改摄像机位置时具有完全的灵活性,使用 CameraUpdateFactory.newCameraPosition(CameraPosition)
它CameraUpdate
可以将摄像机移动到给定位置。A CameraPosition
可以直接获得,使用 new CameraPosition()
或CameraPosition.Builder
使用 new CameraPosition.Builder()
。
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude()), 15));
平移(滚动)
CameraUpdateFactory.scrollBy(float, float)
为您提供一个CameraUpdate
更改相机的纬度和经度,使地图按指定的像素数移动。正x值会使摄像机向右移动,以使地图看起来向左移动。正y值会使相机向下移动,以使地图看起来向上移动。相反,负x值会导致相机向左移动,因此地图似乎已向右移动,负y值会导致相机向上移动。滚动是相对于相机的当前方向。例如,如果摄像机的轴承为90度,则向东“向上”。
onCameraChangeListener
这个回调很重要,我们的很多逻辑可以在这里处理,现在此方法已被废弃,被以下回调替代
GoogleMap.OnCameraMoveStartedListener
GoogleMap.OnCameraMoveListener
GoogleMap.OnCameraIdleListener
目前先写这么多了,后面继续增加地图区域设置,电子围栏功能使用。