我有一个片段,其中加载了谷歌地图上的所有商店位置。但现在我只想加载距离当前位置 5 英里范围内的商店。
这是我的代码:
public class NearMeFragment extends Fragment implements GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener, LocationListener {
GoogleMap map;
LatLng latlng;
private LocationRequest lr;
private LocationClient lc;
MapFragment mapFragment;
//ImageView iv;
private static View view;
Response response;
public NearMeFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (view != null) {
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null)
parent.removeView(view);
}
File file = new File( Environment.getExternalStorageDirectory() + "/json.txt");
if(file.exists()) {
try{
Reader inputStreamReader = new InputStreamReader(new FileInputStream(file));
Gson gson = new Gson();
this.response = gson.fromJson(inputStreamReader, Response.class);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (@SuppressWarnings("hiding") IOException e){
e.printStackTrace();
}
}else{
System.out.println("Error");
}
try {
view = inflater.inflate(R.layout.map_near_me, container,
false);
mapFragment = ((MapFragment) this.getActivity()
.getFragmentManager().findFragmentById(R.id.map));
map = mapFragment.getMap();
map.getUiSettings().setAllGesturesEnabled(true);
map.getUiSettings().setMyLocationButtonEnabled(true);
map.setMyLocationEnabled(true);
map.getUiSettings().setZoomControlsEnabled(true);
for(Shop shop : this.response.shops){
for(int i = 0; i < shop.getShopLat().size(); i++){
map.addMarker(new MarkerOptions().position(new LatLng(Double.parseDouble(shop.getShopLat().get(i)), Double.parseDouble(shop.getShopLng().get(i)))).title(shop.getName()));
}
}
MapsInitializer.initialize(this.getActivity());
} catch (GooglePlayServicesNotAvailableException e) {
Toast.makeText(getActivity(), "Google Play Services missing !",
Toast.LENGTH_LONG).show();
} catch (InflateException e) {
Toast.makeText(getActivity(), "Problems inflating the view !",
Toast.LENGTH_LONG).show();
} catch (NullPointerException e) {
Toast.makeText(getActivity(), "Google Play Services missing !",
Toast.LENGTH_LONG).show();
}
return view;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
lr = LocationRequest.create();
lr.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
lc = new LocationClient(this.getActivity().getApplicationContext(), this, this);
lc.connect();
}
@Override
public void onLocationChanged(Location l2) {
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(l2.getLatitude(), l2.getLongitude()), 15);
map.animateCamera(cameraUpdate);
}
@Override
public void onConnectionFailed(ConnectionResult arg0) {
}
@Override
public void onConnected(Bundle connectionHint) {
lc.requestLocationUpdates(lr, this);
}
@Override
public void onDisconnected() {
}
}
此代码运行良好,但需要时间加载。我认为我的问题可以通过使用一些 if 语句来解决。但想不通怎么弄。有人可以帮我解决吗?此外,每当我启动此活动/片段时,我都会看到非洲的其他一些国家。我如何更改为默认英国?