2

My Dashboard Fragment:

public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
 View rootView = inflater.inflate(R.layout.fragment_dashboard, container, false);

 initActionBar(rootView);
 initView(rootView);
 return rootView;
}

private void initView(View rootView) {

 mViewPager = rootView.findViewById(R.id.viewPager);
 mTab = (TabLayout) rootView.findViewById(R.id.tabLayout);
 addFragment();
 viewPagerAdapter = new ViewPagerAdapter(fragList, getChildFragmentManager());
 mViewPager.setAdapter(viewPagerAdapter);
 mTab.setupWithViewPager(mViewPager);
 mViewPager.setOffscreenPageLimit(3);
 setNavigationView(rootView);
 setDrawerView();
 new Handler().postDelayed(new Runnable() {
  @Override
  public void run() {
   getCurrentLocation();
  }
 }, 3500);

}

private void addFragment() {

 fragList = new ArrayList < Fragment > ();
 fragList.add(HomeFragment.getInstance(Constants.ALL_SPOT));
 fragList.add(HomeFragment.getInstance(Constants.TRAVEL_SPOT));
 fragList.add(HomeFragment.getInstance(Constants.DINNING_SPOT));
 fragList.add(HomeFragment.getInstance(Constants.CONVENIENCES_SPOT));

}
@Override
public void gpsLocationResult(int resultCode, Location location) {

 if (resultCode == GpsLocationConstants.REQUEST_CODE_PERMISSION_DEINED) {
  showRequiredLocationDialog();
 } else {
  if (resultCode == GpsLocationConstants.REQUEST_CODE_PERMISSION_GRANTED && location == null) {
   getCurrentLocation();
  }
  //        mUpdateLocationCallBack.UpdateLocationData(resultCode, location);
  for (int i = 0; i < fragList.size(); i++) {

   HomeFragment homeFragment = (HomeFragment) fragList.get(i);
   homeFragment.gpsLocationResult(resultCode, location);
  }
 }

}

public void getCurrentLocation() {

 ((EasyLocationAppCompatActivity) getActivity()).requestLocation(
  GpsLocationConstants.SINGLE_FIX,
  LocationRequest.PRIORITY_HIGH_ACCURACY,
  3000,
  true);
}

My HomeFragment:

public static HomeFragment getInstance(String Category) {
 HomeFragment myFragment = new HomeFragment();

 Bundle args = new Bundle();
 args.putString(Constants.SPOT_CATEGORY, Category);
 myFragment.setArguments(args);

 return myFragment;
}
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

 rootView = inflater.inflate(R.layout.fragment_home, container, false);

 initView(rootView);

 //  initActionBar(rootView);
 return rootView;
}
private void initViews(rootView) {
 loadMap();
}

private void loadMap() {

 if (getActivity() != null) {
  mapFragment = SupportMapFragment.newInstance();
  getChildFragmentManager().beginTransaction()
   .replace(R.id.home_map_container, mapFragment)
   .commit();
  if (GistUtils.isInternetConnected(getActivity())) {

   mapFragment.getMapAsync(HomeFragment.this);

   //Observable<GoogleMap> mapData = OnMapAndLayoutReady.onMapAndLayoutReadyObservable(mapFragment);

   //mapData.subscribe();


   /*mMap = mapData.toBlocking().first();

   // set map style
   Utils.setMapStyle(getActivity(), mMap);

   hideLocationAndCompassIconsFromMap();

   getCurrentLocation();*/


   //setMapLoadedCallback();
  } else {

   showToast(SyncResourceManager.getString("internet_connection_error"));
  }

 }
}
public void onMapReady(GoogleMap googleMap) {

 //Observable<GoogleMap> mapData = OnMapAndLayoutReady.onMapAndLayoutReadyObservable(mapFragment.getView());
 mMap = googleMap;
 Log.i("HomeFragmentMapLoaded", "" + mMap);
 // set map style
 Utils.setMapStyle(getActivity(), mMap);

 hideLocationAndCompassIconsFromMap();

 //getCurrentLocation();

 /*new Handler().postDelayed(new Runnable() {
     @Override
     public void run() {
         getCurrentLocation();
     }
 }, 3000);*/


 /* mMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
      @Override
      public void onMapLoaded() {

      }
  });*/

 mapFragment.getView().getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
  @Override
  public void onGlobalLayout() {

   mapFragment.getView().getViewTreeObserver().removeOnGlobalLayoutListener(this);

   getCurrentLocation();

   /*new Handler().postDelayed(new Runnable() {
       @Override
       public void run() {

           //getCurrentLocation();
           //testFunc();
       }
   }, 5000);*/
  }
 });

}

this method is called as it received data from database and differeniate the categories of markers according to the instance of fragment and place it

private void addSpotsMarkersAndHandleInfoWindowClickEvents() {

    for (SpotDetail spotItem : mSpotList) {

        if (searchedSpot == null || !(searchedSpot.getPeeSpotId() == spotItem.getPeeSpotId())) {

            if (spotCategory.equals(Constants.CONVENIENCES_SPOT)) {
                if (spotItem.getCategoryId() == 1) {
                    bitmapIcon = BitmapFactory.decodeResource(getResources(), R.drawable.icon_marker_yellow);
                    addMarker(spotItem);
                }
            }
            if (spotCategory.equals(Constants.DINNING_SPOT)) {
                if (spotItem.getCategoryId() == 3) {
                    bitmapIcon = BitmapFactory.decodeResource(getResources(), R.drawable.icon_marker_purple);
                    addMarker(spotItem);
                }
            }
            if (spotCategory.equals(Constants.TRAVEL_SPOT)) {
                if (spotItem.getCategoryId() == 2) {
                    bitmapIcon = BitmapFactory.decodeResource(getResources(), R.drawable.icon_marker_green);
                    addMarker(spotItem);
                }
            }
            if (spotCategory.equals(Constants.ALL_SPOT)) {
                if (spotItem.getCategoryId() == 1) {
                    bitmapIcon = BitmapFactory.decodeResource(getResources(), R.drawable.icon_marker_yellow);
                    addMarker(spotItem);
                }
                if (spotItem.getCategoryId() == 3) {
                    bitmapIcon = BitmapFactory.decodeResource(getResources(), R.drawable.icon_marker_purple);
                    addMarker(spotItem);
                }
                if (spotItem.getCategoryId() == 2) {
                    bitmapIcon = BitmapFactory.decodeResource(getResources(), R.drawable.icon_marker_green);
                    addMarker(spotItem);
                }
            }
        }
    }

    // set adapter
    mMap.setInfoWindowAdapter(new PeeSpotInfoAdapter(getActivity()));

    // handle click when user tap on popup
    mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
        @Override
        public void onInfoWindowClick(Marker marker) {

            // hide spot Info popup
            marker.hideInfoWindow();

            // navigate To Spot Detail View
            SpotDetail spotItem = (SpotDetail) marker.getTag();
            navigateToSpotDetailView(spotItem);
        }
    });

    // long click listener on map when user wants to add new spot
    mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
        @Override
        public void onMapLongClick(LatLng latLng) {

            if (isSpotWithinUserRadius(latLng)) {

                mLatLng = latLng;

                // find address by location using google api
                locationPresenter.getRawData(latLng, HomeFragment.this);

            } else {

                String spotOutsideRadius = SyncResourceManager.getString("filter_screen_txt_spot_outside_radius");
                showToast(spotOutsideRadius);
            }
        }
    });

    // when user tap on spot marker, It shows the popup info
    mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
        @Override
        public boolean onMarkerClick(Marker marker) {

            Object tag = marker.getTag();

            // show info popup only when its spot
            if (tag instanceof SpotDetail) {

                lastOpenMarker = marker;
                return false;
            }

            // disable popup info
            return true;
        }
    });


}

this is addmarker method private void addMarker(final SpotDetail spotItem) {

    String latitude = spotItem.getLatitude();
    String longitude = spotItem.getLongitude();


    if (latitude != null && !latitude.isEmpty() && longitude != null && !longitude.isEmpty()) {

        final LatLng latLng = new LatLng(
                Double.parseDouble(latitude),
                Double.parseDouble(longitude));

        if (spotItem.getPeeSpotIcon() != null && spotItem.getPeeSpotIcon().trim().length() > 0) {
            Target target = new Target() {
                @Override
                public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                    // Add Marker
                    float aFloat = Float.parseFloat(spotItem.getLongitude());
                    Random r = new Random();
                    float random = 3f + r.nextFloat() * (15.2f - 3.15f);
                    float zIndex = aFloat + random;
                    Marker marker = mMap.addMarker(
                            new MarkerOptions()
                                    .position(latLng)
                                    .title(spotItem.getPeeSpotId() + "")
                                    .anchor(0.5f, 0.5f)
                                    .zIndex(zIndex)

// .icon((BitmapDescriptorFactory.fromBitmap(bitmap)))); .icon((BitmapDescriptorFactory.fromBitmap(getMarkerBitmapFromView(bitmap))))); marker.setTag(spotItem); mMarkerList.add(marker); targets.remove(this); Log.i("Targets", "Removing: "); }

                @Override
                public void onBitmapFailed(Drawable errorDrawable) {
                    targets.remove(this);
                }

                @Override
                public void onPrepareLoad(Drawable placeHolderDrawable) {
                    Log.i("Targets", "Preparing: ");
                }
            };
            targets.add(target);
            Picasso.with(getContext()).load(spotItem.getPeeSpotIcon()).into(target);
        } else {
            float aFloat = Float.parseFloat(longitude);
            Random r = new Random();
            float random = 5f + r.nextFloat() * (75.23523f - 5.15413f);
            float zIndex = aFloat + random;
            // Add Marker
            Marker marker = mMap.addMarker(
                    new MarkerOptions()
                            .position(latLng)
                            .title(spotItem.getPeeSpotId() + "")
                            .zIndex(zIndex)
                            .anchor(0.5f, 0.5f)
                            .icon((BitmapDescriptorFactory.fromBitmap(bitmapIcon))));
            marker.setTag(spotItem);
            mMarkerList.add(marker);

        }
    }
}
  • No one gonna help? – Umair Ahmed Qadri Oct 18 '19 at 09:18
  • as i add single Fragment multiple times with different instance in list and then deliver it to viewpager, the marker start jumping , and if i pass only one fragment .... jumps stops..... but i have to pass the fragment 4 times with different instance – Umair Ahmed Qadri Oct 18 '19 at 09:20
  • Can you please post your full code so that we're able to reproduce this from our side? – evan Oct 21 '19 at 07:09
  • Code in comments is hard to read, can you please edit your OP to include all relevant code? For full code you may want to share a github or drive link. Or provide a minimal reproducible sample app that shows this issue. Thx. – evan Oct 22 '19 at 09:31
  • @evan i have put the most code please look after it Thankx :) – Umair Ahmed Qadri Oct 22 '19 at 11:26
  • Thanks I'll take a look and get back to you if I am able to successfully reproduce this! – evan Oct 22 '19 at 13:39
  • The above code cannot be replicated, it does not even include the markers implementation. Can you post code that demonstrates the jumping markers issue somewhere please? – evan Oct 22 '19 at 14:46
  • There are a couple of horrendous bugs/design flaws in Google Maps that come to mind when anyone mentions things jumping around. One is that when you touch a marker, it jumps above your fingertip to make it more visible. The problem is that Google implemented this by actually updating the marker's model, not just its appearance, so if you save it at that point, you're saving the wrong location. The other problem is that if your padding is not symmetrical, the map center moves whenever the device is rotated. By rotating back and forth, you can "walk" the map continuously in one direction. – Kevin Krumwiede Oct 23 '19 at 06:24
  • These bugs have been around for years, so if your problem turns out to be related, your best bet is probably to drop Google Maps for Mapbox. Mapbox isn't without its share of bugs, but the developers are responsive and competent. – Kevin Krumwiede Oct 23 '19 at 06:26
  • have you fixed this issue.if fixed means comment here @UmairAhmedQadri – ABDUL RAHMAN Aug 20 '21 at 06:48
  • Actually the problem was that if was using same googleMap Refrence for each tabs which gives this glitch, so I make different fragments for each tabs along with their separate map instance @ABDULRAHMAN – Umair Ahmed Qadri Aug 10 '22 at 09:34

0 Answers0