从纬度和经度坐标数组列表向 google 地图添加多个标记
Adding multiple markers to google map from array list of latitude and longitude coordinates
目前我有一个应用程序可以在后台获取手机的 GPS 位置并将经度和纬度坐标添加到两个单独的数组列表中,我知道这部分工作正常,但是当我点击按钮绘制指向我的地图,当我的数组列表中有数百个时,它只绘制第一个点
btnPrevious.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for(int i = 0;i<latitude.size();i++)
{
mMap.addMarker(new MarkerOptions().position(new LatLng(latitude.get(i), longitude.get(i))).title("LOCATION " + latitude.get(i) + ", " + longitude.get(i)));
Log.d("LOCATION", latitude.get(i)+ "," + longitude.get(i));
}
Log.d("Count", ""+longitude.size());
}
});
试试这个代码,我正在获取数据列表 class,其中包含我在标记选项中设置的纬度和经度信息以显示在地图上。如果你想为相机设置动画,你可以将构建器实例传递给 animateCamera,地图将为你添加所有已添加标记的动画。
private void insertMarkers(List<Data> list) {
final LatLngBounds.Builder builder = new LatLngBounds.Builder();
for (int i = 0; i < list.size(); i++) {
final Lat Lng position = new LatLng(list.get(i).getCurrent_lat(), list.get(i).getCurrent_lng());
final MarkerOptions options = new MarkerOptions().position(position);
mMaps.addMarker(options);
builder.include(position);
}
}
我这样做是为了在地图上用不同颜色的标记显示电机位置:
private void addMarkersToMap() {
mMap.clear();
for (int i = 0; i < Motors.size(); i++) {
LatLng ll = new LatLng(Motors.get(i).getPos().getLat(), Motors.get(i).getPos().getLon());
BitmapDescriptor bitmapMarker;
switch (Motors.get(i).getState()) {
case 0:
bitmapMarker = BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED);
Log.i(TAG, "RED");
break;
case 1:
bitmapMarker = BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN);
Log.i(TAG, "GREEN");
break;
case 2:
bitmapMarker = BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE);
Log.i(TAG, "ORANGE");
break;
default:
bitmapMarker = BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED);
Log.i(TAG, "DEFAULT");
break;
}
mMarkers.add(mMap.addMarker(new MarkerOptions().position(ll).title(Motors.get(i).getName())
.snippet(getStateString(Motors.get(i).getState())).icon(bitmapMarker)));
Log.i(TAG,"Car number "+i+" was added " +mMarkers.get(mMarkers.size()-1).getId());
}
}
}
Motors 是自定义对象的 ArrayList,mMarkers 是标记的 ArrayList。
注意:您可以像这样在片段中显示地图:
private GoogleMap mMap;
.....
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the
// map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
// Hide the zoom controls as the button panel will cover it.
mMap.getUiSettings().setZoomControlsEnabled(false);
// Add lots of markers to the map.
addMarkersToMap();
// Setting an info window adapter allows us to change the both the
// contents and look of the
// info window.
mMap.setInfoWindowAdapter(new CustomInfoWindowAdapter());
// Set listeners for marker events. See the bottom of this class for
// their behavior.
mMap.setOnMarkerClickListener(this);
mMap.setOnInfoWindowClickListener(this);
mMap.setOnMarkerDragListener(this);
// Pan to see all markers in view.
// Cannot zoom to bounds until the map has a size.
final View mapView = getSupportFragmentManager().findFragmentById(R.id.map).getView();
if (mapView.getViewTreeObserver().isAlive()) {
mapView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@SuppressLint("NewApi")
// We check which build version we are using.
@Override
public void onGlobalLayout() {
LatLngBounds.Builder bld = new LatLngBounds.Builder();
for (int i = 0; i < mAvailableCars.size(); i++) {
LatLng ll = new LatLng(Cars.get(i).getPos().getLat(), Cars.get(i).getPos().getLon());
bld.include(ll);
}
LatLngBounds bounds = bld.build();
mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 70));
mapView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
});
}
}
然后在 onCreate() 中调用 setUpMapIfNeeded()
目前我有一个应用程序可以在后台获取手机的 GPS 位置并将经度和纬度坐标添加到两个单独的数组列表中,我知道这部分工作正常,但是当我点击按钮绘制指向我的地图,当我的数组列表中有数百个时,它只绘制第一个点
btnPrevious.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for(int i = 0;i<latitude.size();i++)
{
mMap.addMarker(new MarkerOptions().position(new LatLng(latitude.get(i), longitude.get(i))).title("LOCATION " + latitude.get(i) + ", " + longitude.get(i)));
Log.d("LOCATION", latitude.get(i)+ "," + longitude.get(i));
}
Log.d("Count", ""+longitude.size());
}
});
试试这个代码,我正在获取数据列表 class,其中包含我在标记选项中设置的纬度和经度信息以显示在地图上。如果你想为相机设置动画,你可以将构建器实例传递给 animateCamera,地图将为你添加所有已添加标记的动画。
private void insertMarkers(List<Data> list) {
final LatLngBounds.Builder builder = new LatLngBounds.Builder();
for (int i = 0; i < list.size(); i++) {
final Lat Lng position = new LatLng(list.get(i).getCurrent_lat(), list.get(i).getCurrent_lng());
final MarkerOptions options = new MarkerOptions().position(position);
mMaps.addMarker(options);
builder.include(position);
}
}
我这样做是为了在地图上用不同颜色的标记显示电机位置:
private void addMarkersToMap() {
mMap.clear();
for (int i = 0; i < Motors.size(); i++) {
LatLng ll = new LatLng(Motors.get(i).getPos().getLat(), Motors.get(i).getPos().getLon());
BitmapDescriptor bitmapMarker;
switch (Motors.get(i).getState()) {
case 0:
bitmapMarker = BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED);
Log.i(TAG, "RED");
break;
case 1:
bitmapMarker = BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN);
Log.i(TAG, "GREEN");
break;
case 2:
bitmapMarker = BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE);
Log.i(TAG, "ORANGE");
break;
default:
bitmapMarker = BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED);
Log.i(TAG, "DEFAULT");
break;
}
mMarkers.add(mMap.addMarker(new MarkerOptions().position(ll).title(Motors.get(i).getName())
.snippet(getStateString(Motors.get(i).getState())).icon(bitmapMarker)));
Log.i(TAG,"Car number "+i+" was added " +mMarkers.get(mMarkers.size()-1).getId());
}
}
}
Motors 是自定义对象的 ArrayList,mMarkers 是标记的 ArrayList。
注意:您可以像这样在片段中显示地图:
private GoogleMap mMap;
.....
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the
// map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
// Hide the zoom controls as the button panel will cover it.
mMap.getUiSettings().setZoomControlsEnabled(false);
// Add lots of markers to the map.
addMarkersToMap();
// Setting an info window adapter allows us to change the both the
// contents and look of the
// info window.
mMap.setInfoWindowAdapter(new CustomInfoWindowAdapter());
// Set listeners for marker events. See the bottom of this class for
// their behavior.
mMap.setOnMarkerClickListener(this);
mMap.setOnInfoWindowClickListener(this);
mMap.setOnMarkerDragListener(this);
// Pan to see all markers in view.
// Cannot zoom to bounds until the map has a size.
final View mapView = getSupportFragmentManager().findFragmentById(R.id.map).getView();
if (mapView.getViewTreeObserver().isAlive()) {
mapView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@SuppressLint("NewApi")
// We check which build version we are using.
@Override
public void onGlobalLayout() {
LatLngBounds.Builder bld = new LatLngBounds.Builder();
for (int i = 0; i < mAvailableCars.size(); i++) {
LatLng ll = new LatLng(Cars.get(i).getPos().getLat(), Cars.get(i).getPos().getLon());
bld.include(ll);
}
LatLngBounds bounds = bld.build();
mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 70));
mapView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
});
}
}
然后在 onCreate() 中调用 setUpMapIfNeeded()