i want to show marker on google map by getting longitude and latitude from json..if i use marker in AsyncTask it is not showing marker at given latlng... i am here so far...thanks in advance....
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;
}}
对于这个问题,在github上给出了示例代码,尝试这个
You need to add the markers on the UI thread, so you will need to call the method to add markers from within 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);
}
}
});
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.