我試圖製作一個應用程序,向用戶顯示最近的健身房。到目前爲止,該應用程序下載最近的健身房的名稱和位置。我不知道如何做的是將經度和緯度分配給它所屬的地名。當JSON是輸出到日誌,它出來的: 地名 地方座標 newPlaceName newPlacecoordinates如何在Google Places Api中爲其相應的地名指定經度和緯度?
我如何分配座標的地名,所以我可以稍後再引用它們? 這裏是我的代碼:
public class MainActivity extends AppCompatActivity implements LocationListener {
int REQUEST_PLACE_PICKER;
LocationManager locationManager;
String provider;
DownloadTask task;
Double lat;
Double lng;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
task = new DownloadTask();
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
provider = locationManager.getBestProvider(new Criteria(), false);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
Location location = locationManager.getLastKnownLocation(provider);
if (location != null){
Toast.makeText(getApplicationContext(), "yay", Toast.LENGTH_SHORT).show();
lat = location.getLatitude();
lng = location.getLongitude();
Log.e("Location info: Lat", lat.toString());
Log.e("Location info: Lng", lng.toString());
}
else{
Toast.makeText(getApplicationContext(), "damn", Toast.LENGTH_SHORT).show();
}
task.execute("https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=" + String.valueOf(lat) + "," + String.valueOf(lng) + "&radius=50000&type=gym&&key=MYAPIKEY");
}
@Override
public void onLocationChanged(Location location) {
Double lat = location.getLatitude();
Double lng = location.getLongitude();
Log.i("Location info: Lat", lat.toString());
Log.i("Location info: Lng", lng.toString());
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
public class DownloadTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
String result = "";
URL url;
HttpURLConnection urlConnection;
try {
url = new URL(urls[0]);
urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = urlConnection.getInputStream();
InputStreamReader reader = new InputStreamReader(in);
int data = reader.read();
while (data != -1) {
char current = (char) data;
result += current;
data = reader.read();
}
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
try {
JSONObject jsonObject = new JSONObject(result);
String weatherContent = jsonObject.getString("results");
Log.i("gymresults", weatherContent);
JSONArray jsonArray = new JSONArray(weatherContent);
for (int i = 0; i < jsonArray.length(); i++){
JSONObject jsonPart = jsonArray.getJSONObject(i);
Log.i("gymname", jsonPart.getString("name"));
Log.i("gymlocation", jsonPart.getString("geometry"));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}