简体   繁体   中英

Showing some number on snackbar when I am getting location from Location listener

I am using a map and location listener. I am calling the location listener when the map is ready and showing marker's on map after getting the location from location listener , all this in setMap() method.

When I am getting the location I see some numbers on snackbar , I dont know from where it is coming, nowhere I am showing it. I am showing snackbar's to show the internet and gps alert. But this is coming from nowhere.

Looks like this:

在此处输入图片说明

What is it??

Code:

public class SearchMerchantFragment extends Fragment implements GetSearchedMerchantsAsyncTask.GetSearchedMerchantsCallBack, OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

    LocationListener[] mLocationListeners = new LocationListener[]{
            new LocationListener(LocationManager.NETWORK_PROVIDER),
            new LocationListener(LocationManager.GPS_PROVIDER)

    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_search_merchant, container, false);

        setUpUI(view);

        return view;
    }


    public void setUpUI(View view) {

        setUpMapIfNeeded();


        sessionData = new SessionData(getActivity());
        sessionUserId = sessionData.getString("user_id", "-1");
        access_token = sessionData.getString("api_key", "-1");


        mLocationsList = new ArrayList<>();
        markers = new ArrayList<>();

        edtSearch = (EditText) view.findViewById(R.id.edtSearch);
        parentLayout = (RelativeLayout) view.findViewById(R.id.parentLayout);
        rv_fetch_merchants = (RecyclerView) view.findViewById(R.id.rv_fetch_merchants);
        merchantsList = new ArrayList<Merchants>();
        merchantsAdapter = new SearchedMerchantsAdapter(this.getContext(), merchantsList);
        rv_fetch_merchants.setLayoutManager(new LinearLayoutManager(getActivity()));
        rv_fetch_merchants.setAdapter(merchantsAdapter);
        rv_fetch_merchants.setHasFixedSize(true);
        rv_fetch_merchants.setItemViewCacheSize(30);
        rv_fetch_merchants.setDrawingCacheEnabled(true);
        rv_fetch_merchants.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);


    }

    //get searched merchants
    public void accessMerchants() {

       new GetSearchedMerchantsAsyncTask(getActivity(), SearchMerchantFragment.this).execute(access_token, sessionUserId, String.valueOf(mLastLocation.getLatitude()), String.valueOf(mLastLocation.getLongitude()));

    }

    @Override
    public void doPostExecute(ArrayList<Merchants> merchantsArrayList) {

        merchantsList.clear();
        merchantsList.addAll(merchantsArrayList);
        merchantsAdapter.notifyDataSetChanged();

        for (Merchants merchants : merchantsList) {
            LatLng latLng = new LatLng(merchants.getLatitude(), merchants.getLongitude());
            MarkerOptions marker = new MarkerOptions().position(latLng).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)).title(merchants.getKirana_name());
            Marker m = mGoogleMap.addMarker(marker);
            markers.add(m);
        }

    }

    public void showAlert(String alert) {

        Snackbar snackbar = Snackbar.make(parentLayout, alert, Snackbar.LENGTH_LONG);
        snackbar.show(); // Don’t forget to show!
    }

    private void setUpMapIfNeeded() {
        if (mGoogleMap == null) {
            mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map_fragment);
            mapFragment.getMapAsync(this);

        }
    }

    //setup map

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mGoogleMap = googleMap;

        buildGoogleApiClient();

        mGoogleApiClient.connect();

        mapConnected = true;

        if (mapConnected) {
            if (CommonUtils.isConnectedToInternet(getActivity())) {

                if(ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                    initializeLocationManager();
                    requestLocation();
                }
                else {
                    ((HomeActivity) getActivity()).showAlert(getResources().getString(R.string.locationAlert));
                }
            } else {
                ((HomeActivity) getActivity()).showAlert(getResources().getString(R.string.check_network));
            }
        }


    }

    protected synchronized void buildGoogleApiClient() {
        //   Toast.makeText(getActivity(),"buildGoogleApiClient",Toast.LENGTH_SHORT).show();
        mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    }

    @Override
    public void onConnected(Bundle bundle) {
        // Toast.makeText(getActivity(),"onConnected",Toast.LENGTH_SHORT).show();


    }

    @Override
    public void onConnectionSuspended(int i) {
        //  Toast.makeText(getActivity(),"onConnectionSuspended",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        //    Toast.makeText(getActivity(),"onConnectionFailed",Toast.LENGTH_SHORT).show();
    }


    @Override
    public void onDetach() {
        super.onDetach();
        mapConnected = false;
        mGoogleMap = null;

        View view = getActivity().getCurrentFocus();
        if (view != null) {
            InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }

    public void setMap() {

        try {      

                    mLatLang = new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude());
                    accessMerchants();


                    MarkerOptions markerOptions = new MarkerOptions();
                    markerOptions.position(mLatLang);
                    markerOptions.title(getResources().getString(R.string.position));
                    markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE));
                    mMarker = mGoogleMap.addMarker(markerOptions);

                    CameraPosition cameraPosition = new CameraPosition.Builder().target(mLatLang).zoom(14).build();

                    mGoogleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));



                    mGoogleMap.setMyLocationEnabled(true);
                    mGoogleMap.getUiSettings().setZoomControlsEnabled(true);
                    mGoogleMap.getUiSettings().setAllGesturesEnabled(true);


                    mLocationManager.removeUpdates(mLocationListeners[0]);
                    mLocationManager.removeUpdates(mLocationListeners[1]);


        } catch (SecurityException e) {

        }
    }

    private void initializeLocationManager() {

        if (mLocationManager == null) {
            mLocationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
        }

        boolean gps_enabled = false;
        boolean network_enabled = false;

        try {
            gps_enabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        } catch (Exception ex) {
        }

        try {
            network_enabled = mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        } catch (Exception ex) {
        }

        if (!gps_enabled && !network_enabled) {
            // notify user


            ((HomeActivity) getActivity()).showAlert(getResources().getString(R.string.locationAlert));
        }

    }

    public class LocationListener implements android.location.LocationListener {

        public LocationListener() {
        }

        public LocationListener(String provider) {
            Log.e(Application.TAG, "LocationListener " + provider);
            mLastLocation = new Location(provider);
        }

        @Override
        public void onLocationChanged(Location location) {
            Log.e(Application.TAG, "onLocationChanged: " + location);

            mLastLocation.set(location);

            if(mapConnected)
            setMap();

        }

        @Override
        public void onProviderDisabled(String provider) {
            Log.e(Application.TAG, "onProviderDisabled: " + provider);

        }
        @Override
        public void onProviderEnabled(String provider) {
            Log.e(Application.TAG, "onProviderEnabled: " + provider);

        }
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            Log.e(Application.TAG, "onStatusChanged: " + provider);
        }
    }

    public void requestLocation() {

        try {
            mLocationManager.requestLocationUpdates(
                    LocationManager.NETWORK_PROVIDER, 0, 0,
                    mLocationListeners[0]);
        } catch (java.lang.SecurityException ex) {
            Log.i(Application.TAG, "fail to request location update, ignore", ex);
        } catch (IllegalArgumentException ex) {
            Log.d(Application.TAG, "network provider does not exist, " + ex.getMessage());
        }

        try {
            mLocationManager.requestLocationUpdates(
                    LocationManager.GPS_PROVIDER, 0, 0,
                    mLocationListeners[1]);
        } catch (java.lang.SecurityException ex) {
            Log.i(Application.TAG, "fail to request location update, ignore", ex);
        } catch (IllegalArgumentException ex) {
            Log.d(Application.TAG, "gps provider does not exist " + ex.getMessage());
        }
    }
}

As I did debug I found that I get this in onLocationChanged method when the setMap() method is called, before or after the method is called.

Which number is this?? Please help thank you..

Replace this in your code:

getString(R.string.locationAlert);

With this.

getResources().getString(R.string.locationAlert);

The issue is getString(R.string.locationAlert); this code returns the R file integer assigned space.

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