public class MapsActivity extends AppCompatActivity implements LocationListener {
GoogleMap map;
private boolean flag = true;
private ProgressDialog mainDialog;
private MarkerOptions mp; ///my point
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
mainDialog = ProgressDialog.show(this, "Wait a bit", "...", true);
map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
mainDialog.dismiss();
Toast.makeText(getApplicationContext(), "We are not allow to take your current location.", Toast.LENGTH_LONG).show();
return;
}
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
}
@Override
public void onLocationChanged(Location location) {
final LatLng MOUNTAIN_VIEW = new LatLng(location.getLatitude(), location.getLongitude());
if (flag && location != null) {
map.clear();
mp = new MarkerOptions();
mp.position(new LatLng(location.getLatitude(), location.getLongitude()));
mp.title("my position");
map.addMarker(mp);
// map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 10));
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(MOUNTAIN_VIEW) // Sets the center of the map to Mountain View
.zoom(10) // Sets the zoom
.bearing(90) // Sets the orientation of the camera to east
.tilt(30) // Sets the tilt of the camera to 30 degrees
.build(); // Creates a CameraPosition from the builder
map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
mainDialog.dismiss();
new getMosque().execute(new LatLng(location.getLatitude(), location.getLongitude()));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_map, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case R.id.action_refresh:
new getMosque().execute(mp.getPosition());
break;
}
return true;
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
class getMosque extends AsyncTask<LatLng, String, LatLng> {
double x, y;
String name;
private ProgressDialog dialog;
private LatLng myPoint;
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(MapsActivity.this);
dialog.setTitle("Please Wait");
dialog.show();
}
@TargetApi(Build.VERSION_CODES.KITKAT)
@Override
protected LatLng doInBackground(LatLng... params) {
myPoint = params[0];
String s = "https://maps.googleapis.com/maps/api/place/nearbysearch" +
"/json?location=" + myPoint.latitude + "," + myPoint.longitude
+ "&radius=50000&types=mosque&name=ireland" +
"&key=AIzaSyDP_2efAut2LHEHU4xc6_oe-1Yf-x-EKKA";
Log.i("app", s);
try {
URL url = new URL(s);
BufferedReader r = new BufferedReader(new InputStreamReader(((HttpURLConnection) url.openConnection()).getInputStream()));
JsonParser jp = new JsonParser();
JsonElement jsonElement = jp.parse(r);
JsonObject jsonObject = jsonElement.getAsJsonObject();
Log.i("app", "doInBackground: json = " + jsonObject.toString());
JsonArray jsonArray = jsonObject.getAsJsonArray("results");
for (JsonElement element : jsonArray) {
name = element.getAsJsonObject().get("name").getAsString();
if (name.contains("Islamic Cultural Centre of Ireland")) {
x = Double.valueOf(element.getAsJsonObject().get("geometry").getAsJsonObject().get("location").getAsJsonObject().get("lat").getAsString());
//Log.i("app", "doInBackground: x = " + x);
y = Double.valueOf(element.getAsJsonObject().get("geometry").getAsJsonObject().get("location").getAsJsonObject().get("lng").getAsString());
break;
}
}
return new LatLng(x, y);
}
catch (Exception e) {
e.printStackTrace();
return null;
}
}
@Override
protected void onPostExecute(LatLng latLng) {
super.onPostExecute(latLng);
dialog.dismiss();
MarkerOptions mp = new MarkerOptions();
mp.position(latLng);
mp.title(name);
mp.icon(BitmapDescriptorFactory.fromResource(R.drawable.masjid));
map.addMarker(mp);
flag = false;
if (latLng != null) {
mp.position(latLng);
mp.title(name);
mp.icon(BitmapDescriptorFactory.fromResource(R.drawable.masjid));
map.addMarker(mp);
flag = false;
}
else {
Toast.makeText(getApplicationContext(), "Sorry, but we didn't find any embassy near you.", Toast.LENGTH_LONG).show();
}
}
}
}
function to getmosque near to my current location
您好我的问题是要从我当前的位置获取兴趣点。在我的情况下,我试图找到我当前位置附近的清真寺。我添加了两张图片来解释我是如何从当前位置获取清真寺的。我能够显示我当前的位置,并且我能够使用(如果有的话)声明在清真寺上展示。我正在使用json来获得所有的清真寺,但我只能展示一个。我想从json向我附近的所有清真寺展示。请帮我解决这个问题。提前致谢
答案 0 :(得分:0)
半径和变量
Location mLastLocation;
Marker mCurrLocationMarker;
LocationRequest mLocationRequest;
private int PROXIMITY_RADIUS = 5000;
onLocationChange方法:
@Override
public void onLocationChanged(Location location) {
mLastLocation = location;
if (mCurrLocationMarker != null) {
mCurrLocationMarker.remove();
}
//Place current location marker
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title("Current Position");
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
//mCurrLocationMarker = mMap.addMarker(markerOptions);
String Mosque = "mosque";
Log.d("onClick", "Button is Clicked");
mMap.clear();
String url = getUrl(mLastLocation.getLatitude(), mLastLocation.getLongitude(), Mosque);
Object[] DataTransfer = new Object[2];
DataTransfer[0] = mMap;
DataTransfer[1] = url;
Log.d("onClick", url);
GetNearbyPlacesData getNearbyPlacesData = new GetNearbyPlacesData();
getNearbyPlacesData.execute(DataTransfer);
//move map camera
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(11));
//stop location updates
if (mGoogleApiClient != null) {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}
}
这是getUrl方法:
private String getUrl(double latitude, double longitude, String nearbyPlace) {
StringBuilder googlePlacesUrl = new StringBuilder("https://maps.googleapis.com/maps/api/place/nearbysearch/json?");
googlePlacesUrl.append("location=" + latitude + "," + longitude);
googlePlacesUrl.append("&radius=" + PROXIMITY_RADIUS);
googlePlacesUrl.append("&types=" + nearbyPlace);
googlePlacesUrl.append("&sensor=true");
googlePlacesUrl.append("&key=" + "AIzaSyBt7_eLLSV66gAbwMPvv8bqp5b9QklPYzc");
Log.d("getUrl", googlePlacesUrl.toString());
return (googlePlacesUrl.toString());
}
答案 1 :(得分:0)
for 循环包含一个 break,它在第一次迭代后跳出 for 循环,因此您只能显示 1 条记录。