简体   繁体   中英

Google map api not showing all the markers on the screen when the markers are too much distant

I'm trying to show a polyline but I can't get the markers of the map to fit on my phone screen.

I have 2 markers in the GoogleMap.

If the two markers are not very distant like, everything work fine, the two markers are visible on the screen.

But if the two markers are very distant like, the two markers are not visible on the screen.

Below is my code:

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();
            }
        });

How to make all the makers visible on the screen?

How to make all the makers fit on the screen?

What should I do?

Thanks.

Update your code with the following:

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);
                        }
                    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM