【问题标题】:Android - google map - how show multiple markers on the map?Android - 谷歌地图 - 如何在地图上显示多个标记?
【发布时间】:2016-07-25 08:21:56
【问题描述】:

我想在谷歌地图上显示多个标记(可能是 100 或更多)。我正在从 sqlite 读取 latlng 然后我将它们添加到谷歌地图上,但 只显示一个标记 ?

我必须使用 Thread 还是 AsyncTask

这是我的代码:

public class StartActivity extends AppCompatActivity implements OnMapReadyCallback {

    private GoogleMap mMap;
    public SQLiteDatabase sql;
    public Cursor cursor;
    Context context;
    public static String PACKAGE_NAME;
    ArrayList<LatLng> markerPoints;
    LatLng newLatLng;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

        context = getApplicationContext();
        DBS db = new DBS(context);
        PACKAGE_NAME = context.getApplicationContext().getPackageName();
        db.GetPackageName(PACKAGE_NAME);
        db.CreateFile();
        try {
            db.CreateandOpenDataBase();
        } catch (IOException e) {
            e.printStackTrace();
        }
        sql = db.openDataBase();

        try {
            cursor = sql.rawQuery("SELECT _Lat,_Lng,_Date,_Time FROM Places where UserCode = 101", null);
            if (cursor != null) {
                if (cursor.moveToFirst()) {
                    do {
                        markerPoints = new ArrayList<LatLng>();
                        Structure_DB sdb = new Structure_DB();
                        sdb._Lat = cursor.getString(cursor.getColumnIndex("_Lat"));
                        sdb._Lng = cursor.getString(cursor.getColumnIndex("_Lng"));
                        sdb._Date = cursor.getString(cursor.getColumnIndex("_Date"));
                        sdb._Time = cursor.getString(cursor.getColumnIndex("_Time"));
                        newLatLng = new LatLng(Double.parseDouble(sdb._Lat), Double.parseDouble(sdb._Lng));
                        markerPoints.add(newLatLng);
                    } while (cursor.moveToNext());
                }
            }
        } catch (Exception e) {
        } finally {
            cursor.close();
        }
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        Iterator<LatLng> iterator = markerPoints.iterator();
        while (iterator.hasNext()) {
            MarkerOptions markerOptions = new MarkerOptions()
                    .position(iterator.next());
            mMap.addMarker(markerOptions);
        }
    }
}

【问题讨论】:

标签: android google-maps


【解决方案1】:

您正在每次迭代中初始化您的markerPoints

从 do-while 循环中删除 markerPoints = new ArrayList&lt;LatLng&gt;(); 并在声明 ArrayList&lt;LatLng&gt; markerPoints = new ArrayList&lt;LatLng&gt;(); 上对其进行初始化

【讨论】:

  • antonio,我的观点是,当我运行我的应用程序时,应用程序太慢了。我该怎么办?我必须使用 Thread 还是 AsyncTask ...?
  • 您可以(并且必须)使用AsyncTask 来查询数据库,但不幸的是,标记绘制必须在主(UI)线程上完成,并且是一项非常繁重的操作。提高速度的唯一选择是尝试减少正在绘制的标记(地图元素)的数量。例如使用标记聚类 (developers.google.com/maps/documentation/android-api/utility/…)
  • 安东尼奥,非常感谢。
猜你喜欢
  • 2012-11-14
  • 1970-01-01
  • 2016-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-11
  • 2017-11-04
相关资源
最近更新 更多