谷歌地图:在mapview中动态画布绘制

发布于 2025-01-04 10:00:20 字数 1942 浏览 0 评论 0原文

我目前在地图视图上动态添加标记时遇到麻烦,这肯定是由于我缺乏 java 知识:(

我应该给画布提供哪个参数才能完成这项工作? 当我输入坐标时,地图会转到我想要的地方,但

((Button)findViewById(R.id.goMap)).setOnClickListener(
                      new OnClickListener() {
                     @Override
                     public void onClick(View v) {



                            mapView.invalidate();
                        // On récupère notre EditText
                         EditText UserName = ((EditText)findViewById(R.id.getLon));
                         EditText Password = ((EditText)findViewById(R.id.getLat));

                         // On garde la chaîne de caractères
                          _lat = UserName.getText().toString();
                          _long = Password.getText().toString();

                         latTest =  Double.parseDouble(_lat)* 1E6;
                         longTest =  Double.parseDouble(_long)* 1E6;

                         p3 = new GeoPoint(
                                    (int) (latTest ), 
                                  (int) (longTest ));

                          //---add the marker---
                          Bitmap bmp3 = BitmapFactory.decodeResource(
                               getResources(), R.drawable.maps_position_marker); 
                          Canvas canvas= new Canvas();
                           canvas.drawBitmap(bmp3, screenP3ts.x-15, screenP3ts.y-30, null);


                         mapView.getProjection().toPixels(p3, screenP3ts);
                            mapController.animateTo(p3);
                            mapController.setCenter(p3);

                         Toast.makeText(TheMap.this, "lat=" + latTest + " et " + "long= " + longTest, Toast.LENGTH_SHORT).show();


                         }
                         });

我的初始代码中没有制造商,覆盖层具有所需的所有良好参数,已加载到 onCreate () 方法中并且可以正常工作很好,但正如我提到的,我不知道如何在 onclick () 方法中添加标记。我知道我做错了,但我不知道该怎么做:(

提前谢谢

i'm currently having trouble to add dynamicly a marquer on my mapview, certainly due to my lack in java knowledge :(

which parameter should i give to my canvas to make this work?
when i fee the coordinates, the map goes where i want but i have no maker

((Button)findViewById(R.id.goMap)).setOnClickListener(
                      new OnClickListener() {
                     @Override
                     public void onClick(View v) {



                            mapView.invalidate();
                        // On récupère notre EditText
                         EditText UserName = ((EditText)findViewById(R.id.getLon));
                         EditText Password = ((EditText)findViewById(R.id.getLat));

                         // On garde la chaîne de caractères
                          _lat = UserName.getText().toString();
                          _long = Password.getText().toString();

                         latTest =  Double.parseDouble(_lat)* 1E6;
                         longTest =  Double.parseDouble(_long)* 1E6;

                         p3 = new GeoPoint(
                                    (int) (latTest ), 
                                  (int) (longTest ));

                          //---add the marker---
                          Bitmap bmp3 = BitmapFactory.decodeResource(
                               getResources(), R.drawable.maps_position_marker); 
                          Canvas canvas= new Canvas();
                           canvas.drawBitmap(bmp3, screenP3ts.x-15, screenP3ts.y-30, null);


                         mapView.getProjection().toPixels(p3, screenP3ts);
                            mapController.animateTo(p3);
                            mapController.setCenter(p3);

                         Toast.makeText(TheMap.this, "lat=" + latTest + " et " + "long= " + longTest, Toast.LENGTH_SHORT).show();


                         }
                         });

in my intial code, the overlay which has all good parametres it needs, is loaded in the onCreate () methode and it works fine but as i mentioned i can't figure out how to add the marker in the onclick () methode. I know what i did is wrong but i don't know how to do :(

thx in advance

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

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

发布评论

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

评论(1

相思碎 2025-01-11 10:00:20

在实际将纬度/经度转换为屏幕坐标之前,您正在使用 screenP3ts 绘制到画布上。你需要这个:

Bitmap bmp3 = BitmapFactory.decodeResource(
        getResources(), R.drawable.maps_position_marker); 
Canvas canvas= new Canvas();

mapView.getProjection().toPixels(p3, screenP3ts);
canvas.drawBitmap(bmp3, screenP3ts.x-15, screenP3ts.y-30, null);

You are drawing to the canvas using screenP3ts before you actually transform the lat / lon into screen coordinates. You need this:

Bitmap bmp3 = BitmapFactory.decodeResource(
        getResources(), R.drawable.maps_position_marker); 
Canvas canvas= new Canvas();

mapView.getProjection().toPixels(p3, screenP3ts);
canvas.drawBitmap(bmp3, screenP3ts.x-15, screenP3ts.y-30, null);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文