我有谷歌地图android的问题,我想从json显示我的坐标。 我的杰森:
{"info":[{"nama":"Masjid Agung","0":"Masjid Agung","deskripsi":"tempat sholat umat Isalm","1":"tempat sholat umat Isalm","lng":"106.332725524902","2":"106.332725524902","lat":"-6.23728179931641","3":"-6.23728179931641"},{"nama":"Restoran Bersama","0":"Restoran Bersama","deskripsi":"temapt makan","1":"temapt makan","lng":"106.309807206177","2":"106.309807206177","lat":"-6.23967002769392","3":"-6.23967002769392"},{"nama":"Restoran Cepat saja","0":"Restoran Cepat saja","deskripsi":"enak nih di sini","1":"enak nih di sini","lng":"106.318476105713","2":"106.318476105713","lat":"-6.22431180265454","3":"-6.22431180265454"},{"nama":"Bandara Halim","0":"Bandara Halim","deskripsi":"benarkah tidak ini bandara","1":"benarkah tidak ini bandara","lng":"106.34568443396","2":"106.34568443396","lat":"-6.24939667082029","3":"-6.24939667082029"},{"nama":"Bandara ABC","0":"Bandara ABC","deskripsi":"Ini adalah bandara","1":"Ini adalah bandara","lng":"106.331951523804","2":"106.331951523804","lat":"-6.22013087470731","3":"-6.22013087470731"}]}
这是我的主要活动:
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MainActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
任何人都可以帮我如何自定义onMapReady函数来显示json的坐标。
由于
答案 0 :(得分:1)
以下是工作代码:
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
// Fetch data
fetchDataFromJSON();
}
private void fetchDataFromJSON()
{
// JSON response
String strResponse = "{\"info\":[{\"nama\":\"Masjid Agung\",\"0\":\"Masjid Agung\",\"deskripsi\":\"......";
try
{
JSONObject response = new JSONObject(strResponse);
JSONArray jsonArray = response.getJSONArray("info");
// Clear old markers
mMap.clear();
// Looping through all info and show on map
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String name = jsonObject.getString("nama");
String description = jsonObject.getString("deskripsi");
String latitude = jsonObject.getString("lat");
String longitude = jsonObject.getString("lng");
// Add marker
addMarkerToMap(latitude, longitude, name, description);
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(mContext, "Error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
public void addMarkerToMap(String latitude, String longitude, String title, String description)
{
double lat = Double.parseDouble(latitude);
double lng = Double.parseDouble(longitude);
// create marker
MarkerOptions marker = new MarkerOptions().position(new LatLng(lat, lng))
.title(title)
.snippet(description);
// Marker icon
marker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
// Add marker to map
mMap.addMarker(marker);
}
希望这会有所帮助〜
答案 1 :(得分:0)
您必须使用json字符串创建一个JsonObject,循环遍历它并获取必需的字段。