Ich möchte Marker auf Google-Karte zeigen durch Longitude und Breitengrad von JSON .. Wenn ich Marker in AsyncTask verwenden, zeigt es nicht Marker bei gegebenen latlng. .. ich bin hier so weit ... Vielen dank im Voraus ....Ich möchte Marker auf Google-Karte zeigen Längen- und Breitengrad von JSON
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;
}}
brother..it zeigen nichts – Abhi
Die logcat keine Fehler hat oder Warnungen, aber die Markierungen werden nicht angezeigt. – Abhi
Werden die Markierungen am selben Ort hinzugefügt, an dem Ihre Karte initialisiert wird? In der Methode onMapReady verschieben Sie die Karte nach Sydney. – Joyson