如何在谷歌地图中实际显示项目的位置?

发布于 2024-10-03 08:09:00 字数 2060 浏览 0 评论 0原文

我有一个谷歌地图应用程序,显示两个项目(itemizedoverlay 类),并且我有一个更新按钮。当我按下更新按钮时,我想从地图中删除项目,并再次放置相同的项目,但具有新的纬度和经度值(我有一项服务,可以实现共享首选项中的纬度和经度字段,并且我也通过捆绑重新获得其他位置)

我做错了什么?当我尝试我的代码时,地图的项目不会被删除......只是出现新项目......但我想删除旧的项目

updateButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                mapOverlays.clear();
                updateMyPosition();
                updateFriendPosition();
            }
        });

,这是我的两个功能:

private void updateFriendPosition() 
    {
        Bundle bundle = this.getIntent().getExtras();//get the intent & bundle passed by X
        userText.setText(bundle.getString("user"));
        permissionText.setText(bundle.getString("permission"));
        lastUpdateText.setText(bundle.getString("lastupdate"));
        String coordinates[] = {bundle.getString("lat"), bundle.getString("lon")};  
        lat = Double.parseDouble(coordinates[0]);
        lng = Double.parseDouble(coordinates[1]);
        p = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
        OverlayItem overlayitem1 = new OverlayItem(p, bundle.getString("user"), "Hi Friend!");
        itemizedoverlay.addOverlay(overlayitem1);
        mapOverlays.add(itemizedoverlay);// dibujo la estrella con la posicion actual del friend

        mc.animateTo(p); ///nos centra el mapa en la posicion donde esta nuestro amigo
        mc.setZoom(10);  /// ajusta el zoom a 10        
    }

private void updateMyPosition()
    {
        String coordinates[] = {settings.getString("mylatitude", null),settings.getString("mylongitude", null)};    
        lat = Double.parseDouble(coordinates[0]);
        lng = Double.parseDouble(coordinates[1]);
        p = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
        OverlayItem overlayitem1 = new OverlayItem(p, "Me", "My Position");
        itemizedoverlay2.addOverlay(overlayitem1);
        mapOverlays.add(itemizedoverlay2);//dibujo mi icono para mostrarme a mi
    }

我还尝试了 invalidate()地图视图,但它不起作用...仍然绘制旧项目...

i have a googlemap app that displays two items (itemizedoverlay class) and i have a Update button. When i press the update button, i want to remove the items from the map, and put again the same items but with the new latitude and longitude values (i have a service that actualice the latitude and longitude fields that i have in sharedPreferences, and also i recibe the other position by a bundle)

what i am doing wrong? when i try my code, the items of the map doesn't be removed.... just new items appear... but i want to remove the old ones

updateButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                mapOverlays.clear();
                updateMyPosition();
                updateFriendPosition();
            }
        });

and these are my two functions:

private void updateFriendPosition() 
    {
        Bundle bundle = this.getIntent().getExtras();//get the intent & bundle passed by X
        userText.setText(bundle.getString("user"));
        permissionText.setText(bundle.getString("permission"));
        lastUpdateText.setText(bundle.getString("lastupdate"));
        String coordinates[] = {bundle.getString("lat"), bundle.getString("lon")};  
        lat = Double.parseDouble(coordinates[0]);
        lng = Double.parseDouble(coordinates[1]);
        p = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
        OverlayItem overlayitem1 = new OverlayItem(p, bundle.getString("user"), "Hi Friend!");
        itemizedoverlay.addOverlay(overlayitem1);
        mapOverlays.add(itemizedoverlay);// dibujo la estrella con la posicion actual del friend

        mc.animateTo(p); ///nos centra el mapa en la posicion donde esta nuestro amigo
        mc.setZoom(10);  /// ajusta el zoom a 10        
    }

private void updateMyPosition()
    {
        String coordinates[] = {settings.getString("mylatitude", null),settings.getString("mylongitude", null)};    
        lat = Double.parseDouble(coordinates[0]);
        lng = Double.parseDouble(coordinates[1]);
        p = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
        OverlayItem overlayitem1 = new OverlayItem(p, "Me", "My Position");
        itemizedoverlay2.addOverlay(overlayitem1);
        mapOverlays.add(itemizedoverlay2);//dibujo mi icono para mostrarme a mi
    }

i also tryed with invalidate() on the mapview but it doesn't works... still paint the old items...

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

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

发布评论

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

评论(1

梦巷 2024-10-10 08:09:01

看起来这是基于SDK教程的。您已向 itemizedoverlay 添加了两件事,另一件事是。需要在HelloItemizedOverlay中添加一个方法,即 'clear'

public void clear() {
    mOverlays.clear();
}

调用前调用该方法

itemizedoverlay.addOverlay(overlayitem1);

It looks like this is based on the SDK tutorial. You've added two things to itemizedoverlay and the other one. You need to add a method in HelloItemizedOverlay, namely 'clear'

public void clear() {
    mOverlays.clear();
}

Call this method before you call

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