我想通过从json获取经度和纬度来显示谷歌地图上的标记..如果我在AsyncTask中使用标记它在给定的latlng中没有显示标记... 我到目前为止......提前感谢....
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
String city = "New Delhi";
String location = null;
double lat;
double lng ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
new JSONTask().execute("http://www.xxxxxxx.com/bar/mob.php");
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng latLng = new LatLng(-30, 140);
mMap.addMarker(new MarkerOptions().position(latLng).title("Marker"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
// i want use that lat and lng here which i am getting in AsyncTask
LatLng latLng1 = new LatLng(lat, lng);
mMap.addMarker(new MarkerOptions().position(latLng1).title("Marker"));
mMap.setMyLocationEnabled(true);
}
private class JSONTask extends AsyncTask<String, String, List<Bardata>> {
@Override
protected List<Bardata> doInBackground(String... params) {
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
URL url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.setReadTimeout(10000);
connection.setConnectTimeout(15000);
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
Uri.Builder builder = new Uri.Builder()
.appendQueryParameter("location", location)
.appendQueryParameter("city", city);
String query = builder.build().getEncodedQuery();
OutputStream os = connection.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(query);
writer.flush();
writer.close();
os.close();
connection.connect();
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
String finalJson = buffer.toString();
System.out.println("Returned value " + finalJson.toString());
JSONObject parentObject = new JSONObject(finalJson);
JSONArray parentArray = parentObject.getJSONArray("home");
List<Bardata> bardataList = new ArrayList<>();
Gson gson = new Gson();
for (int i = 0; i < parentArray.length(); i++) {
JSONObject finalObject = parentArray.getJSONObject(i);
Bardata barData = gson.fromJson(finalObject.toString(), Bardata.class);
bardataList.add(barData);
lat = barData.getLatitude();
lng = barData.getLongitude();
/* LatLng latLng1 = new LatLng(lat, lng);
mMap.addMarker(new MarkerOptions().position(latLng1).title("Marker"));
mMap.setMyLocationEnabled(true);*/
}
return bardataList;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(final List<Bardata> result) {
super.onPostExecute(result);
if (result != null) {
} else {
Toast.makeText(getApplicationContext(), "Currently,we are not present in your city", Toast.LENGTH_SHORT).show();
}
}
}}
bardata.java
public class Bardata {
private String barname;
private double longitude;
private double latitude;
public double getLongitude() {
return longitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
public double getLatitude() {
return latitude;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public String getBarname() {
return barname;
}
public void setBarname(String barname) {
this.barname = barname;
}}
答案 0 :(得分:0)
对于这个问题,github上给出了示例代码 试试this
答案 1 :(得分:0)
您需要在UI线程上添加标记,因此您需要调用方法以在runOnUiThread中添加标记
for (int i = 0; i < parentArray.length(); i++) {
JSONObject finalObject = parentArray.getJSONObject(i);
Bardata barData = gson.fromJson(finalObject.toString(), Bardata.class);
bardataList.add(barData);
lat = barData.getLatitude();
lng = barData.getLongitude();
LatLng latLng1 = new LatLng(lat, lng);
runOnUiThread(new Runnable() {
public void run() {
try {
mMap.addMarker(new MarkerOptions().position(latLng1).title("Marker"));
mMap.setMyLocationEnabled(true);
} catch (JSONException e) {
Log.e(LOG_TAG, "Error adding marker", e);
}
}
});
}