android-如何在离线的JSON文件中显示地图上的标记?

时间:2017-03-16 19:37:57

标签: android json google-maps google-maps-api-3 google-maps-markers

我想离线从json文件资源文件夹加载标记并在地图上显示它。 以前我可以将标记显示为textview,但我无法在地图上显示它们。 该应用程序显示地图但不显示标记。

我不知道为什么。

stations.json

 [
    {
        "name": "JAVANMARDAN1",
        "lat": 35.747394,
        "lang": 51.267577
    }
]

map.xml

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"/>

MapsActivity

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
    private Marker marker;
    private GoogleMap mMap;

    public MapsActivity() throws JSONException {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

        new AsyncTaskGetMareker().execute();


    }

    public String getJSONFromAssets() {
        String json = null;
        try {
            InputStream inputData = getAssets().open("stations.json");
            int size = inputData.available();
            byte[] buffer = new byte[size];
            inputData.read(buffer);
            inputData.close();
            json = new String(buffer, "UTF-8");
        } catch (IOException ex) {
            ex.printStackTrace();
            return null;
        }
        return json;

    }


    private class AsyncTaskGetMareker extends AsyncTask
    <String , String, JSONArray>{
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected JSONArray doInBackground(String... strings) {
            return null;
        }

        protected void onPostExecute (JSONArray result){
            if (result !=null){
                for (int i =0; i <result.length(); i++){
                    JSONObject jsonObject= null;
                    try {
                        jsonObject= result.getJSONObject(i);
                        String name=jsonObject.getString("name");
                        String lat=jsonObject.getString("lat");
                        String lang=jsonObject.getString("lang");


                        drawMarker(new LatLng(Double.parseDouble(lat),
                                Double.parseDouble(lang)));

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }

        }

        private void drawMarker(LatLng point) {
            MarkerOptions markerOptions = new MarkerOptions();
            markerOptions.position(point);
            GoogleMap googleMap = null;
            googleMap.addMarker(markerOptions);
        }




    }


    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
    }
}

1 个答案:

答案 0 :(得分:1)

您的代码中存在多个问题:

  • 您未从AsyncTask
  • 中调用getJSONFromAssets()
  • 您在加载地图之前执行AsyncTask
  • drawMarker()方法中,您需要创建新的GoogleMap参考并将其设置为null。这不会奏效。
  • 您的代码忽略了name字段,我认为您希望将其作为InfoWindow的代码段。

这是更新后的代码,修复了上述所有问题:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
    private Marker marker;
    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        new AsyncTaskGetMareker().execute();
    }

    public String getJSONFromAssets() {
        String json = null;
        try {
            InputStream inputData = getAssets().open("stations.json");
            int size = inputData.available();
            byte[] buffer = new byte[size];
            inputData.read(buffer);
            inputData.close();
            json = new String(buffer, "UTF-8");
        } catch (IOException ex) {
            ex.printStackTrace();
            return null;
        }
        return json;
    }

    private class AsyncTaskGetMareker extends AsyncTask<String , String, JSONArray> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected JSONArray doInBackground(String... strings) {
            String stationsJsonString = getJSONFromAssets();
            try {
                JSONArray stationsJsonArray = new JSONArray(stationsJsonString);
                return stationsJsonArray;
            } catch (JSONException e) {
                e.printStackTrace();
            }
            //This will only happen if an exception is thrown above:
            return null;
        }

        protected void onPostExecute (JSONArray result){
            if (result !=null){
                for (int i =0; i <result.length(); i++){
                    JSONObject jsonObject= null;
                    try {
                        jsonObject= result.getJSONObject(i);
                        String name=jsonObject.getString("name");
                        String lat=jsonObject.getString("lat");
                        String lang=jsonObject.getString("lang");

                        drawMarker(new LatLng(Double.parseDouble(lat),
                                Double.parseDouble(lang)), name);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }

        }

        private void drawMarker(LatLng point, String name) {
            MarkerOptions markerOptions = new MarkerOptions();
            markerOptions.position(point);
            markerOptions.snippet(name);
            mMap.addMarker(markerOptions);
        }
    }

}