简体   繁体   English

从位置侦听器获取位置时,在小吃店上显示一些数字

[英]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. 当地图准备就绪时,我正在调用位置侦听器,并从位置侦听器获取位置之后,在setMap()方法中将位置显示在地图上。

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. 当我到达该位置时,我在snackbar上看到一些数字,我不知道它来自哪里,没有地方显示它。 I am showing snackbar's to show the internet and gps alert. 我正在显示小吃店以显示互联网和gps警报。 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. onLocationChanged调试时,我发现在onLocationChanged setMap()方法之前或之后,我在onLocationChanged方法中获得了此方法。

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); 问题是getString(R.string.locationAlert); this code returns the R file integer assigned space. 此代码返回R文件整数分配的空间。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在没有听众的情况下获取鼠标位置 - getting mouse location without a listener 从Activity调用位置时获取NullPointerException - Getting NullPointerException when call Location from Activity 我正在尝试从JAVA中的IP地址获取位置信息,但是却遇到了java.net.SocketException:连接重置错误 - I am trying to get location information from ip address in JAVA but I am getting a java.net.SocketException: Connection reset error 不知道为什么我得到这个NullPointerException应该在这个位置有数据 - No clue why I am getting this NullPointerException there should be data at this location 当我在其他应用程序上时,如何在活动中更新我的位置 - How to update my location on activity when I am on other application (TestNg)当我更改驱动程序位置的基础 class 中的位置时,它没有反映它显示 IllegalStateException? - (TestNg) When i change the location in base class of driver location it is not reflecting it is showing IllegalStateException? 我从Firebase获取数据并在TextView中显示它但我收到了一些错误。 任何人都可以检查我的代码并建议我 - I am getting data from Firebase and showing it in the TextView but I am getting some error. Can anyone please check out my code and suggest me 尝试访问当前位置时出现错误和应用程序崩溃 - Getting Error and App crash When I try to Access current location 进度对话框位置侦听器 - Progress Dialog location listener 从后台删除应用程序时也无法获取位置更新 - Not getting location updates when App is removed from background also
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM