【问题标题】:Google map api not showing all the markers on the screen when the markers are too much distant当标记距离太远时,Google map api不会在屏幕上显示所有标记
【发布时间】:2021-08-09 19:45:21
【问题描述】:

我正在尝试显示折线,但我无法让地图的标记适合我的手机屏幕。

我在 GoogleMap 中有 2 个标记。

如果两个标记距离不是很远,一切正常,两个标记在屏幕上可见。

但是如果这两个标记距离很远,那么这两个标记在屏幕上是不可见的。

下面是我的代码:

static final LatLng HAMBURG = new LatLng(46.227638, 2.213749); // for example
static final LatLng HAMBURG_2 = new LatLng(-44.339565,147.637862); // for example

MapView map;
map = findViewById(R.id.mapView);

        map.onCreate(null);
        map.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(final GoogleMap googleMap) {
               

                 googleMap.addMarker(new MarkerOptions()
                         .position(HAMBURG)
                         .title("First Pit Stop")
                         .icon(BitmapDescriptorFactory
                                 .defaultMarker(BitmapDescriptorFactory.HUE_RED)));
                googleMap.addMarker(new MarkerOptions()
                        .position(HAMBURG_2)
                        .title("Wrong Turn!")
                        .icon(BitmapDescriptorFactory
                                .defaultMarker(BitmapDescriptorFactory.HUE_RED)));
             

                 googleMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
                    @Override
                    public void onMapLoaded() {
              

                        PolylineOptions options = new PolylineOptions().width(5).color(Color.BLACK);
                        options.add(pos_1, pos_2);
                        googleMap.addPolyline(options);
                    }
                });

                map.onResume();
            }
        });

如何让所有的制造商在屏幕上可见?

如何让所有的制造商都适合屏幕?

我该怎么办?

谢谢。

【问题讨论】:

    标签: java google-maps google-maps-android-api-2


    【解决方案1】:

    使用以下内容更新您的代码:

    int padding = 0;
        
                        LatLngBounds.Builder builder = new LatLngBounds.Builder();
                        builder.include(pos_1);
                        builder.include(pos_2);
                        LatLngBounds bounds = builder.build();
                         final CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
                         googleMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
                            @Override
                            public void onMapLoaded() {
                                 googleMap.animateCamera(cu);
        
                                PolylineOptions options = new PolylineOptions().width(5).color(Color.BLACK);
                                options.add(pos_1, pos_2);
                                googleMap.addPolyline(options);
                            }
                        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-12
      • 1970-01-01
      • 1970-01-01
      • 2012-08-02
      • 1970-01-01
      • 2016-11-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多