在每次位置更新时在地图上添加标记
adding a marker on a map with each location update
我有一个 android 应用程序可以获取我的设备位置并添加标记。它还跟踪位置和标记移动。每次位置发生变化时,我都想在地图上添加一个标记,这样以后我就可以在它们之间绘制折线。我没有使用 firebase 或 DBHelper。只是在寻找一种简单的方法来做到这一点。
这是onLocationChanged函数
@Override
public void onLocationChanged(Location location) {
LatLng myCoordinates = new LatLng(location.getLatitude(), location.getLongitude());
LocationMarker.setPosition(myCoordinates);
mMap.moveCamera(CameraUpdateFactory.newLatLng(myCoordinates));
float zoomLevel = 12.0f;
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(myCoordinates, zoomLevel));
}
这是我的 onCreate 函数
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tracking);
mOptions = new MarkerOptions().position(new LatLng(0, 0)).title("My Current Location")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE));
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
checkLocationPermission();
}
// Gets map fragment and allows map to display location
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(map);
mapFragment.getMapAsync(this);
}
是否可以将纬度和经度存储到数组中并使用数组绘制点?我一直在努力解决这个问题,但没有确定的
您要找的是 Polyline
,文档可以在 here.
找到
Polyline
的生成器需要 LatLng
个对象。我不确定你目前在做什么,但你可以将 onLocationChanged
函数中的 LatLng
对象存储在一个数组中,或者将你放置在地图上的所有 Markers
对象存储在一个数组中并调用他们的 getLocation
函数来获取 Polyline
.
所需的 LatLng
对象
创建字段变量:
ArrayList<LatLng> points = new ArrayList<>();
并在 onLocationChanged 中执行:
points.add(myCoordinates);
然后当你想创建 PolyLine 时,做类似的事情:
Polyline line = map.addPolyline(new PolylineOptions()
.addAll(points)
.width(5)
.color(Color.RED));
addAll 方法支持 ArrayList 实现的 Iterable 类型。
我有一个 android 应用程序可以获取我的设备位置并添加标记。它还跟踪位置和标记移动。每次位置发生变化时,我都想在地图上添加一个标记,这样以后我就可以在它们之间绘制折线。我没有使用 firebase 或 DBHelper。只是在寻找一种简单的方法来做到这一点。
这是onLocationChanged函数
@Override
public void onLocationChanged(Location location) {
LatLng myCoordinates = new LatLng(location.getLatitude(), location.getLongitude());
LocationMarker.setPosition(myCoordinates);
mMap.moveCamera(CameraUpdateFactory.newLatLng(myCoordinates));
float zoomLevel = 12.0f;
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(myCoordinates, zoomLevel));
}
这是我的 onCreate 函数
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tracking);
mOptions = new MarkerOptions().position(new LatLng(0, 0)).title("My Current Location")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE));
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
checkLocationPermission();
}
// Gets map fragment and allows map to display location
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(map);
mapFragment.getMapAsync(this);
}
是否可以将纬度和经度存储到数组中并使用数组绘制点?我一直在努力解决这个问题,但没有确定的
您要找的是 Polyline
,文档可以在 here.
Polyline
的生成器需要 LatLng
个对象。我不确定你目前在做什么,但你可以将 onLocationChanged
函数中的 LatLng
对象存储在一个数组中,或者将你放置在地图上的所有 Markers
对象存储在一个数组中并调用他们的 getLocation
函数来获取 Polyline
.
LatLng
对象
创建字段变量:
ArrayList<LatLng> points = new ArrayList<>();
并在 onLocationChanged 中执行:
points.add(myCoordinates);
然后当你想创建 PolyLine 时,做类似的事情:
Polyline line = map.addPolyline(new PolylineOptions()
.addAll(points)
.width(5)
.color(Color.RED));
addAll 方法支持 ArrayList 实现的 Iterable 类型。