从纬度和经度坐标的数组列表中为谷歌地图添加多个标记

时间:2015-05-05 13:37:26

标签: android google-maps google-maps-markers

目前我有一个应用程序可以在背景中获取手机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());


        }
    });

2 个答案:

答案 0 :(得分:5)

尝试此代码,我将获取一个Data类列表,其中包含我在Marker选项中设置的纬度和经度信息,以便在地图上显示。如果要为相机设置动画,可以将构建器实例传递给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);
        }

    }

答案 1 :(得分:0)

我这样做是为了在地图上显示带有不同颜色标记的电机位置:

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()