gpt4 book ai didi

java - 当我从位置监听器获取位置时在 snackbar 上显示一些数字

转载 作者:搜寻专家 更新时间:2023-11-01 08:30:05 24 4
gpt4 key购买 nike

我正在使用 map 和位置监听器。我在 map 准备就绪时调用位置监听器,并在从位置监听器获取位置后在 map 上显示标记,所有这些都在 setMap() 方法中。

当我获取位置时,我在 snackbar 上看到一些数字,我不知道它来自哪里,我没有在任何地方显示它。我正在展示 snackbar 以显示互联网和全局定位系统警报。但这不知从何而来。

看起来像这样:

enter image description here

这是什么??

代码:

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

当我进行调试时,我发现在调用 setMap() 方法时,在调用该方法之前或之后,我在 onLocationChanged 方法中得到了这个。

这是几号??请帮忙谢谢..

最佳答案

在您的代码中替换它:

getString(R.string.locationAlert);

有了这个。

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

问题是 getString(R.string.locationAlert); 此代码返回 R 文件整数分配空间。

关于java - 当我从位置监听器获取位置时在 snackbar 上显示一些数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41692368/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com