我如何从 sqlite 数据库查询纬度/经度值并显示在 Google 地图上

发布于 2024-11-03 13:18:34 字数 1813 浏览 0 评论 0原文

如何从数据库查询纬度/经度值并在该位置显示标记? 这是我的代码,当我设置纬度/经度时它工作正常

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.branchmap);
    mapView = (MapView) findViewById(R.id.mapView1);


      mc = mapView.getController();
          String[] coordinates = {"13.694317","100.601592"};
            double lat = Double.parseDouble(coordinates[0]);
            double lng = Double.parseDouble(coordinates[1]);

            p = new GeoPoint(
                    (int) (lat * 1E6), 
                    (int) (lng * 1E6));
            mc.animateTo(p);
            mc.setZoom(17); 


            mapOverlays = mapView.getOverlays();
            drawable = this.getResources().getDrawable(R.drawable.mark1);
            itemizedOverlay = new HelloItemizedOverlay(drawable);
            OverlayItem overlayitem = new OverlayItem(p, "", "");
            itemizedOverlay.addOverlay(overlayitem);
            mapOverlays.add(itemizedOverlay);

,但是当我想查询它时它不起作用,我应该把这部分代码放在哪里

try {
        ChannelDatabaseHelper dbHelper = new  ChannelDatabaseHelper(this.getApplicationContext());
        newDB = dbHelper.getWritableDatabase();
        Cursor c1 = newDB.rawQuery("SELECT lat,long FROM " +
                tableName , null);

        if (c1 != null ) {
            if  (c1.moveToFirst()) {
                do {

                    latitude =Integer.parseInt((c1.getString(c1.getColumnIndex("lat"))));
                    longitude = Integer.parseInt((c1.getString(c1.getColumnIndex("long"))));
                    p = new GeoPoint(
                            (int)(latitude *1E6 ), 
                            (int)(longitude *1E6 ));
                }while (c1.moveToNext());
            } 
        } 

How can i query lat/long value from database and display the marker on that location?
this is my code, it works fine when i set lat/long

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.branchmap);
    mapView = (MapView) findViewById(R.id.mapView1);


      mc = mapView.getController();
          String[] coordinates = {"13.694317","100.601592"};
            double lat = Double.parseDouble(coordinates[0]);
            double lng = Double.parseDouble(coordinates[1]);

            p = new GeoPoint(
                    (int) (lat * 1E6), 
                    (int) (lng * 1E6));
            mc.animateTo(p);
            mc.setZoom(17); 


            mapOverlays = mapView.getOverlays();
            drawable = this.getResources().getDrawable(R.drawable.mark1);
            itemizedOverlay = new HelloItemizedOverlay(drawable);
            OverlayItem overlayitem = new OverlayItem(p, "", "");
            itemizedOverlay.addOverlay(overlayitem);
            mapOverlays.add(itemizedOverlay);

but when i want to query it doesn't work, where should i put this part of code

try {
        ChannelDatabaseHelper dbHelper = new  ChannelDatabaseHelper(this.getApplicationContext());
        newDB = dbHelper.getWritableDatabase();
        Cursor c1 = newDB.rawQuery("SELECT lat,long FROM " +
                tableName , null);

        if (c1 != null ) {
            if  (c1.moveToFirst()) {
                do {

                    latitude =Integer.parseInt((c1.getString(c1.getColumnIndex("lat"))));
                    longitude = Integer.parseInt((c1.getString(c1.getColumnIndex("long"))));
                    p = new GeoPoint(
                            (int)(latitude *1E6 ), 
                            (int)(longitude *1E6 ));
                }while (c1.moveToNext());
            } 
        } 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜光 2024-11-10 13:18:34
try {
        ChannelDatabaseHelper dbHelper = new  ChannelDatabaseHelper(this.getApplicationContext());
        newDB = dbHelper.getWritableDatabase();
        Cursor c1 = newDB.rawQuery("SELECT lat,long FROM " +
                tableName , null);

        if (c1 != null ) {
            if  (c1.moveToFirst()) {
                do {

                    latitude =Integer.parseDouble((c1.getString(c1.getColumnIndex("lat"))));
                    longitude = Integer.parseDouble((c1.getString(c1.getColumnIndex("long"))));
                    p = new GeoPoint(
                            (int)(latitude *1E6 ), 
                            (int)(longitude *1E6 ));
                    //Write a code to display this point on google-map
                }while (c1.moveToNext());
            } 
        } 

试试这个代码

try {
        ChannelDatabaseHelper dbHelper = new  ChannelDatabaseHelper(this.getApplicationContext());
        newDB = dbHelper.getWritableDatabase();
        Cursor c1 = newDB.rawQuery("SELECT lat,long FROM " +
                tableName , null);

        if (c1 != null ) {
            if  (c1.moveToFirst()) {
                do {

                    latitude =Integer.parseDouble((c1.getString(c1.getColumnIndex("lat"))));
                    longitude = Integer.parseDouble((c1.getString(c1.getColumnIndex("long"))));
                    p = new GeoPoint(
                            (int)(latitude *1E6 ), 
                            (int)(longitude *1E6 ));
                    //Write a code to display this point on google-map
                }while (c1.moveToNext());
            } 
        } 

Try this code

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文