Description: I am making a small application that displays markers but there is a problem with using JSON.

Problem: I do not understand how to transfer data from a file, for later creating markers based on them. Most of the examples I found were for transferring data from sites (I am new to Android for Android studio here:

.json:

{ "markerss":[ { "title": "West", "lat": 53.223798, "lng": 44.889040, "snippet": "placeinformation2010" }, { "title": "East", "lat": 53.2220, "lng": 44.8799, "snippet": "placeinformation102" } ] } 

.java:

 public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { GoogleMap mMap = null; private static final LatLng place1 = new LatLng(53.223798, 44.889040); private static final LatLng place2 = new LatLng(53.2220, 44.8799); // Решил попробовать сделать перемененные для последующей и работы ,тип ближайшей Marker mPlace1; Marker mPlace2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_maps); // Obtain the SupportMapFragment and get notified when the map is ready to be used. SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() .findFragmentById(map); mapFragment.getMapAsync(this); } @Override public void onMapReady(GoogleMap googleMap) { mMap = googleMap; mMap.moveCamera(CameraUpdateFactory.newLatLngZoom( new LatLng(41.889, -87.622), 10)); // Создание маркеров и подробная их подробная информация mPlace1 = mMap.addMarker(new MarkerOptions() .position(place1) .title("West") .anchor(0.0f, 1.0f) .icon(BitmapDescriptorFactory.fromResource(R.drawable.kebab)) .snippet("dsf")); mMap.moveCamera(CameraUpdateFactory.newLatLng(place1)); mPlace2 = mMap.addMarker(new MarkerOptions() .position(place2) .title("East") .anchor(0.0f, 1.0f) .icon(BitmapDescriptorFactory.fromResource(R.drawable.kebab)) .snippet("edfjk")); mMap.moveCamera(CameraUpdateFactory.newLatLng(place2)); } } 
  • one
    I would like to know at what stage you are having a problem and WHY DID you have a problem: reading from a file? - Michael Rebrov
  • That's right, the problem with reading the file and placing markers on these values. - Prostoimbo

1 answer 1

If I understand you correctly, then you want to store the JSON string in the phone’s memory and work locally.

In this case, you must store your coordinates.json file in the assets folder and read it from there if necessary.

Here is the method for getting the JSON string from the assets folder:

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

This method returns the string Json , that is, you can assign it to your variable, for example:

 String coordinatesJson = loadJSONFromAsset(); JSONObject jsonObject = new JSONObject(coordinatesJson); 

And jsonObject

Example:

  String coordinatesJson = loadJSONFromAsset(); JSONObject jsonObject; try { jsonObject = new JSONObject(coordinatesJson); JSONArray jsonArray = jsonObject.getJSONArray("markerss"); for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObject = jsonArray.getJSONObject(i); String title = jsonObject.getString("title")); Double lat = jsonObject.getDouble("lat")); Double lon = jsonObject.getDouble("lon")); String snippet = jsonObject.getString("snippet")); } } catch (JSONException e) { e.printStackTrace(); } } 

Try it, on this editor wrote, the main thing you understand the essence ...

  • I will assume that for the final solution of the problem, most likely it will also require the conversion of a json string into an object. - Mikhail Rebrov
  • You are right, I assume that json strings can be used to create markers on the map. - Prostoimbo
  • So already written. Use Double lat for Latitude and lon as Longitude. - DevOma