简体   繁体   中英

How to add multiple markers on google maps fragment with json in android studio

I need to add multiple markers on google maps in fragment activity.

This is my current code:

 public View onCreateView(@NonNull LayoutInflater inflater,
                          ViewGroup container, Bundle savedInstanceState) {
     galleryViewModel =
             new ViewModelProvider(this).get(GalleryViewModel.class);
     View view = inflater.inflate(R.layout.fragment_gallery, container, false);

     arrayList = new ArrayList<>();

     SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager()
             .findFragmentById(R.id.map);
     mapFragment.getMapAsync(this::onMapReady);
     
     ProgressDialog progressDialog = new ProgressDialog(getActivity());
     progressDialog.setMessage("Chargemnt des données en cours...");
     progressDialog.show();

     RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
     String url="http://emc2-cloud.com:8079/API/login";
     StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
         @Override
         public void onResponse(String response) {
             progressDialog.dismiss();
             JSONObject responselog2 = null;
             try {
                 responselog2 = new JSONObject(response);
                 JSONObject logincolier = responselog2.getJSONObject("colier");
                /* latitude_maison = logincolier.getString("latitude_maison");
                 longitude_maison = logincolier.getString("longitude_maison");*/

                 liste_colier = logincolier.getJSONArray("donnes_colier");
                 for (int k = 0; k<liste_colier.length(); k++){

                     JSONObject colierliste = liste_colier.getJSONObject(k);

                     detail_distance = colierliste.getString("distance");
                     vitesse = colierliste.getString("vitesse");
                     detail_latitude = colierliste.getDouble("latitude");
                     nom_colier = colierliste.getString("nom_colier");
                     detail_longitude = colierliste.getDouble("longitude");
                     etat_colier = colierliste.getString("etat_colier");                        
                 }
             } catch (JSONException e) {
                 e.printStackTrace();
             }

         }
     }, new Response.ErrorListener() {
         @Override
         public void onErrorResponse(VolleyError error) {
             test.setTextColor(Color.RED);
             test.setText("Connexion échouée");
         }
     }){
         @Override
         protected Map<String,String> getParams(){
             Map<String,String> params=new HashMap<String, String>();
             params.put("username",identifiant.getText().toString());
             params.put("password",mot_de_passe.getText().toString());
             return params;
         }

     };
     requestQueue.add(stringRequest);

 public void onMapReady(GoogleMap googleMap) {
      mMap = googleMap;
      customMarker = BitmapDescriptorFactory.fromResource(R.drawable.teteboeuf);

     colier = new LatLng(detail_latitude,detail_longitude);

      mMap.addMarker(new MarkerOptions().position(colier).title(MainActivity.nom_colier).snippet("Vitessedu colier : "+MainActivity.vitesse+" km").icon(customMarker));
      mMap.moveCamera(CameraUpdateFactory.newLatLng(colier));
      mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
          @Override
          public void onInfoWindowClick(Marker marker) {
              Intent intent = new Intent(getActivity(),DetailColier.class);
              startActivity(intent);
          }
      });
 }

You can do it like this,

                for (int k = 0; k<liste_colier.length(); k++){

                    JSONObject colierliste = liste_colier.getJSONObject(k);

                    detail_distance = colierliste.getString("distance");
                    vitesse = colierliste.getString("vitesse");
                    detail_latitude = colierliste.getDouble("latitude");
                    nom_colier = colierliste.getString("nom_colier");
                    detail_longitude = colierliste.getDouble("longitude");
                    etat_colier = colierliste.getString("etat_colier");
                    addMarker(detail_latitude ,detail_longitude)

                }

Define this method in your activity.

void addMarker(Double latitude, Double longitude){
     LatLng latLong = new LatLng(Double latitude, Double longitude)
     if(mMap!=null{
            mMap.addMarker(new MarkerOptions().position(latLong))
     }
}

For adding title, and other information to the marker you can refer to Marker Official Google Doc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM