6

while loading google map on device i am receiving below screen sometimes.it comes on second load as shown below.google map otherwise it comes perfectly as normal google map with route I am using SupportmapFragment and get googleMap object as.

 supportMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map_view_fragment);

below is code for displaying map in activity/fragment

public static void drawRouteIntoMap(final List<? extends MapHelper> position, final GoogleMap googleMap) {
        /*List<MapHelper> position = new ArrayList<MapHelper>();
        for (int i = lastPosition; i < maps.size(); i++) {
            position.add(maps.get(i));
        }*/
        final LatLngBounds.Builder mapBounds = new LatLngBounds.Builder();
        if (position.size() > 0 && Validator.isNotNull(googleMap)) {
            googleMap.clear();
            List<PolylineOptions> polylineOptionses = new ArrayList<PolylineOptions>();
            PolylineOptions option = null;
            Boolean lastPause = null;
            for (MapHelper map : position) {
                if (map.isPause()) {
                    if (Validator.isNull(lastPause) || !lastPause) {
                        option = new PolylineOptions().width(5).color(Color.rgb(255, 0, 155)).geodesic(true);
                        polylineOptionses.add(option);
                    }
                    mapBounds.include(new LatLng(map.getLatitude(),map.getLongitude()));
                    option.add(new LatLng(map.getLatitude(), map.getLongitude()));
                } else {
                    if (Validator.isNull(lastPause) || lastPause) {
                        option = new PolylineOptions().width(5).color(Color.rgb(0, 179, 253)).geodesic(true);
                        polylineOptionses.add(option);
                    }
                    mapBounds.include(new LatLng(map.getLatitude(),map.getLongitude()));
                    option.add(new LatLng(map.getLatitude(), map.getLongitude()));
                }
                lastPause = map.isPause();
            }
            for (PolylineOptions options : polylineOptionses) {
                googleMap.addPolyline(options);
            }
            if(Validator.isNotNull(option)){
                //List<LatLng> points = option.getPoints();


                googleMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
                    @Override
                    public void onMapLoaded() {
                        LatLng startPoint = new LatLng(position.get(0).getLatitude(), position.get(0).getLongitude());
                        googleMap.addMarker(new MarkerOptions().position(startPoint).title("start").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
                        mapBounds.include(startPoint);
                        LatLng endPoint = new LatLng(position.get(position.size() - 1).getLatitude(), position.get(position.size() - 1).getLongitude());
                        mapBounds.include(endPoint);
                        googleMap.addMarker(new MarkerOptions().position(endPoint).title("finish").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
                        googleMap.setPadding(15, 205, 10, 110);
                        googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(mapBounds.build(), 0));
                        //googleMap.animateCamera(CameraUpdateFactory.newLatLngBounds(mapBounds.build(), 10));
                        googleMap.moveCamera(CameraUpdateFactory.zoomOut());

                    }
                });
            }

        }
    }


 supportMapFragment.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(GoogleMap googleMap) {
                if (Validator.isNotNull(googleMap)) {
                    googleMap.setMyLocationEnabled(false);
                    googleMap.clear();
                    googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
                        @Override
                        public void onMapClick(LatLng latLng) {
                            if (activity.preferences.isSaveScreen()) {
                                facebook.setChecked(false);
                                twitter.setChecked(false);
                                activity.replaceFragment(new FullMapFragment(), null);
                            }
                            if (!activity.preferences.isSaveScreen()) {
                                activity.preferences.setHistoryScreen(true);
                                activity.replaceFragment(new FullMapFragment(), null);
                            }
                        }
                    });
                    if (gps) {
                        nonGpsSummary.setVisibility(View.GONE);
                        List<HistoryMap> historyMaps = new ArrayList<HistoryMap>();
                        if (Validator.isNotNull(activity.preferences.getUserHistory().getHistoryMaps())) {
                            historyMaps.addAll(Arrays.asList(activity.preferences.getUserHistory().getHistoryMaps()));
                        }
                        if (historyMaps.size() > 0) {
                            if (Validator.isNotNull(googleMap)) {
                              drawRouteIntoMap(historyMaps, googleMap);
                            }
                        } else {
                            mapFrame.setVisibility(View.GONE);
                        }
                    } else {
                        gpsSummary.setVisibility(View.GONE);
                    }
                }
            }
        });

this question is in relation with zoom over specific route google map. by using that i get proper route with mapbounds but i m not getting why this screen is displaying.

i am not receiving cordinates 0.0 i debug that,api key is also proper.

Community
  • 1
  • 1
Hardik Mehta
  • 867
  • 1
  • 12
  • 20

2 Answers2

5

You are calling moveCamera twice. The first one tries to move the camera but the second one doesn't wait for the first one to end and performs the zoom out.

This may not happen all the times and may behave differently on different devices.

The best option is to set a padding on your CameraUpdateFactory.newLatLngBounds(mapBounds.build(), 0) instead of 0

antonio
  • 18,044
  • 4
  • 45
  • 61
3

This problem occurs when your coordinates are not properly set. Make sure the coordinates that you are using is pointing in the land to get the maps correctly. Another possible reason is your API key is not working, try to generate new API key for this project.

For more information, check these related SO questions.

Community
  • 1
  • 1
KENdi
  • 7,576
  • 2
  • 16
  • 31