我有一个片段,可以加载谷歌地图上的所有商店位置。但现在我只想加载距离当前位置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语句来解决。但无法弄清楚如何。有人可以帮我解决吗?此外,当我发布这个活动/片段时,我看到非洲的其他一些国家。如何更改为英国默认设置?
答案 0 :(得分:0)
这可能会对您有所帮助:
从教程的第3页开始。另外,查看该人已经实现的查询字符串:
String placesSearchStr = "https://maps.googleapis.com/maps/api/place/nearbysearch/" +
"json?location="+lat+","+lng+
"&radius=1000&sensor=true" +
"&key=AIzaSyAXxxoI2aDYyXTUdu3eOTXayrOcWB6F-_I";//ADD KEY
根据您的需要更改半径。请注意,半径值以METERS表示。所以你5英里的价值就像8046.72(根据谷歌,不确定小数位是否有效)。
我希望这可以提供帮助。
答案 1 :(得分:0)
我认为您可以使用Location类的distanceTo()或distanceBetween()方法来查找每个商店与当前位置之间的距离。在Shop循环中的if语句中使用它。