【问题标题】:Adding multiple markers to google map from array list of latitude and longitude coordinates从纬度和经度坐标的数组列表向谷歌地图添加多个标记
【发布时间】:2015-05-05 22:31:35
【问题描述】:

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


        }
    });

【问题讨论】:

  • 你能发布日志吗?

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


【解决方案1】:

试试这个代码,我正在获取一个包含纬度和经度信息的数据类列表,我在标记选项中设置这些信息以显示在地图上。如果您想为相机设置动画,您可以将构建器实例传递给 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);
        }

    }

【讨论】:

  • 您能再解释一下吗?
【解决方案2】:

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

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

【讨论】:

  • 我的想法是在用户运行应用程序并且正在移动时以设定的间隔放置标记,因此每次用户移动最小距离时,都会与其余标记一起放置一个新标记,以便路线是使用标记显示的,这段代码是否允许我这样做?
猜你喜欢
  • 2018-02-17
  • 2012-02-18
  • 2016-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-02
  • 2013-04-17
相关资源
最近更新 更多