[英]How to Foreach the marker in Maps, using retrofit2?
我在地圖 Android 谷歌地圖 API 中的標記有問題
我試過使用 ArrayList foreach,但是 map 上的標記仍然沒有出現,也許我的語法有錯誤
private void Koordinat() {
Call<List<LatlongModel>> sektorCall = latlongInterface.GetLatlong();
sektorCall.enqueue(new Callback<List<LatlongModel>>() {
@Override
public void onResponse(Call<List<LatlongModel>> call, Response<List<LatlongModel>> response) {
if (response.isSuccessful() && response.body() != null) {
listLatlong = response.body();
ArrayList<Double> itemLatlong = new ArrayList<Double>();
for (int x = 0; x < response.body().size(); x++) {
Double latitude = response.body().get(x).getLatitude();
Double longitude = response.body().get(x).getLongitude();
itemLatlong.add(latitude);
itemLatlong.add(longitude);
latLongA = new LatLng(longitude, latitude);
}
mMap.addMarker(new MarkerOptions().position(latLongA)
.title("Citarum")
.snippet("Titik A")
.rotation((float) 3.5)
.icon(bitmapDescriptorFromVector(getApplicationContext(), R.drawable.ic_action_hijau)));
} else {
Toast.makeText(getBaseContext(), "response message"+ response.body().toString(), Toast.LENGTH_LONG).show();
}
}
@Override
public void onFailure(Call<List<LatlongModel>> call, Throwable t) {
t.printStackTrace();
}
});
}
標記來自以下 JSON 響應:
{“id”:1,“titk”:“Titik A1”,“lat”:-6.9155332,“long”:107.4724021,“idSector”:1}
{“id”:“1”,“titk”:“Titik A2”,“lat”:-6.9153333,“long”:107.4725922,“idSector”:1}
我的期望是標記出現在上面的經緯度 JSON 響應的地圖上
假設 nMap 已經初始化,我相信你的 mMap.addMarker 應該在循環內而不是在循環外。 你能檢查一下嗎?
將標記添加到您的 map 應該在 for 循環內部而不是外部完成。 您的代碼應如下所示。
注意:我將 for 循環更改為 foreach 循環以使事情看起來更干凈。
private void Koordinat() {
Call<List<LatlongModel>> sektorCall = latlongInterface.GetLatlong();
sektorCall.enqueue(new Callback<List<LatlongModel>>() {
@Override
public void onResponse(Call<List<LatlongModel>> call, Response<List<LatlongModel>> response) {
if (response.isSuccessful() && response.body() != null) {
listLatlong = response.body();
for (LatlongModel latLongModel : response.body()) {
LatLng latLongA = new LatLng(latLongModel.getLatitude(), latLongModel.getLongitude());
mMap.addMarker(new MarkerOptions().position(latLongA)
.title("Citarum")
.snippet("Titik A")
.rotation((float) 3.5)
.icon(bitmapDescriptorFromVector(getApplicationContext(), R.drawable.ic_action_hijau)));
}
} else {
Toast.makeText(getBaseContext(), "response message"+ response.body().toString(), Toast.LENGTH_LONG).show();
}
}
@Override
public void onFailure(Call<List<LatlongModel>> call, Throwable t) {
t.printStackTrace();
}
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.