如何将TAG添加到每个标记并分别在每个标记上设置UniqueId

时间:2016-04-08 11:02:39

标签: android json performance android-layout android-fragments

  

如何将TAG添加到每个标记并在每个标记上设置UniqueId。   从服务器接收的每个标记的唯一ID   这是DoInBackgroung Code请帮帮我

 for (int i = 0; i < jsonarray.length(); i++) {
               // ModelClass s = LoganSquare.parse(jsonarray.getJSONObject(i).toString(), ModelClass.class);
                ModelClass modelClass = new Gson().fromJson(jsonarray.getJSONObject(i).toString(), ModelClass.class);


                LatLng latLng = new LatLng(Double.parseDouble(modelClass.getLatitude()), Double.parseDouble(modelClass.getLongitude())); // Use your server's methods
                latLngList.add(latLng);
  

以下是添加标记

的代码
private void AddPointer() {
try {

    if (marker != null) {
        mMap.clear();
        Toast.makeText(getApplicationContext(), "Remove", Toast.LENGTH_LONG).show();

    }
    for (LatLng object : latLngList)

        marker = mMap.addMarker(new MarkerOptions().title("User Name").position(object).icon(BitmapDescriptorFactory.fromResource(R.drawable.female4)));

    System.out.println(marker.getPosition() + "  Marker position.......");
} catch (Exception e) {
    Toast.makeText(MainActivity.this, "Error ", Toast.LENGTH_LONG).show();
    // mMap.clear();
}
 }
  

OnpostExecute代码,其中我添加了从服务器收到的Marker,此时我在服务器上有两个标记及其uniqueId&#39;

 protected void onPostExecute(Boolean result) {
    // dialog.cancel();
    // adapter.notifyDataSetChanged();
    Toast.makeText(getApplicationContext(), "Receicve data from server", Toast.LENGTH_LONG).show();

    if (result == false) {
        Toast.makeText(getApplicationContext(), "Unable to fetch data from server", Toast.LENGTH_LONG).show();

    }
    AddPointer();

}
  

这是Model Class

 public class ModelClass {

     @SerializedName("longi")
   public String longitudeServer;

    @SerializedName("lati")
    public String latitudeServer;

     @SerializedName("uniqueid")
      public String uniqueidSserver;

      public ModelClass(){
      }

      public String getLongitude(){
        return longitudeServer;
      }



      public String getLatitude(){
         return latitudeServer;
     }


            public String getUniqueId(){
          return uniqueidSserver;
      }

     }

2 个答案:

答案 0 :(得分:1)

    Try this you can set diffrent marker and give uniqueid or name


    // Prepare Model Class like this way

    public class LocationDetail
    {
    public String longitudeServer;
    public String latitudeServer;
    public String uniqueidSserver;

    public String getLongitudeServer() {
        return longitudeServer;
    }

    public void setLongitudeServer(String longitudeServer) {
        this.longitudeServer = longitudeServer;
    }

    public String getLatitudeServer() {
        return latitudeServer;
    }

    public void setLatitudeServer(String latitudeServer) {
        this.latitudeServer = latitudeServer;
    }

    public String getUniqueidSserver() {
        return uniqueidSserver;
    }

    public void setUniqueidSserver(String uniqueidSserver) {
        this.uniqueidSserver = uniqueidSserver;
    }
}



  //Prepare the arraylist like this

try
    {
        LocationDetail modelclass;
        JSONObject jsonObject = null;
        ArrayList<LocationDetail> locationDetails = new ArrayList<>();

        JSONArray jsonArray = ""; // initilise your server data
        for (int i = 0; i < jsonArray.length(); i++) {
            jsonObject = jsonArray.getJSONObject(i);
            modelclass = new LocationDetail();
            modelclass.setLongitudeServer(Double.parseDouble(jsonObject
                    .getString("Latitude").toString()));
            modelclass.setLatitudeServer(Double.parseDouble(jsonObject
                    .getString("Longitude").toString()));
            modelclass.setUniqueidSserver(jsonObject.getString(
                    "UniqueId").toString());
            list.add(modelclass);
        }
    }
    catch(JSONException e)
    {
        e.printStackTrace();
    }





    //Now pass above Arraylist to method    
    private void showMap(ArrayList<Reach_Us> list) {
                double latitude = 0;
                double longitude = 0;
                try {

                    // Loading map
                    initilizeMap();
                    googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

                    googleMap.setMyLocationEnabled(true);

                    // Enable / Disable zooming controls
                    googleMap.getUiSettings().setZoomControlsEnabled(true);

                    // Enable / Disable my location button
                    googleMap.getUiSettings().setMyLocationButtonEnabled(true);

                    // Enable / Disable Compass icon
                    googleMap.getUiSettings().setCompassEnabled(true);

                    // Enable / Disable Rotate gesture
                    googleMap.getUiSettings().setRotateGesturesEnabled(true);

                    // Enable / Disable zooming functionality
                    googleMap.getUiSettings().setZoomGesturesEnabled(true);

                    // lets place some 10 random markers
                    for (int i = 0; i <= list.size(); i++) {

                        latitude = list.get(i).getLatitude();
                        longitude = list.get(i).getLongitude();

                        // Adding a marker
                        MarkerOptions marker = new MarkerOptions()
                                .position(
                                        new LatLng(list.get(i).getLatitude(), list
                                                .get(i).getLongitude()))

                                .title(i + ":"
                                        + list.get(i).getUniqueidSserver().toString());

                        marker.icon(BitmapDescriptorFactory
                                .defaultMarker(BitmapDescriptorFactory.HUE_YELLOW));

                        googleMap.addMarker(marker);
                        CameraPosition cameraPosition = new CameraPosition.Builder()
                                .target(new LatLng(list.get(i).getLatitude(), list
                                        .get(i).getLongitude())).zoom(15).build();

                        googleMap.animateCamera(CameraUpdateFactory
                                .newCameraPosition(cameraPosition));

                    }

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

答案 1 :(得分:0)

Pravin我喜欢这个。

 protected Boolean doInBackground(String... urls) {
        try {

            //------------------>>

            HttpGet httpGet = new HttpGet(urls[0]);
            HttpClient httpclient = new DefaultHttpClient();
            HttpResponse response = httpclient.execute(httpGet);
            int status = response.getStatusLine().getStatusCode();

            if (status == 200) {
                HttpEntity entity = response.getEntity();
                String data = EntityUtils.toString(entity);


                JSONArray jsonarray = new JSONArray(data);

                latLngList.clear();

                try {
                    LocationDetail modelclass;
                    JSONObject jsonObject = null;
                    ArrayList<LocationDetail> locationDetails = new ArrayList<>();

                    // JSONArray jsonArray = ""; // initilise your server data
                    for (int i = 0; i < jsonarray.length(); i++) {
                        jsonObject = jsonarray.getJSONObject(i);
                        modelclass = new LocationDetail();
                        modelclass.setLongitudeServer(Double.parseDouble(jsonObject.getString("Latitude").toString()));
                        modelclass.setLatitudeServer(Double.parseDouble(jsonObject.getString("Longitude").toString()));
                        modelclass.setUniqueidSserver(jsonObject.getString("UniqueId").toString());

                        list.add(modelclass);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                return true;

            }
  

以及

 private void showMap(ArrayList<LocationDetail> list) {
    double latitude = 0;
    double longitude = 0;
    try {

        // Loading map
        initMap();
        mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

        mMap.setMyLocationEnabled(true);

        // Enable / Disable zooming controls
        mMap.getUiSettings().setZoomControlsEnabled(true);

        // Enable / Disable my location button
        mMap.getUiSettings().setMyLocationButtonEnabled(true);

        // Enable / Disable Compass icon
        mMap.getUiSettings().setCompassEnabled(true);

        // Enable / Disable Rotate gesture
        mMap.getUiSettings().setRotateGesturesEnabled(true);

        // Enable / Disable zooming functionality
        mMap.getUiSettings().setZoomGesturesEnabled(true);

        // lets place some 10 random markers
        for (int i = 0; i <= list.size(); i++) {

            latitude = list.get(i).getLatitudeServer();
            longitude = list.get(i).getLongitudeServer();

            // Adding a marker
            MarkerOptions marker = new MarkerOptions()
                    .position(
                            new LatLng(list.get(i).getLatitudeServer(), list
                                    .get(i).getLongitudeServer()))

                    .title(i + ":"
                            + list.get(i).getUniqueidSserver().toString());

            marker.icon(BitmapDescriptorFactory
                    .defaultMarker(BitmapDescriptorFactory.HUE_YELLOW));

            mMap.addMarker(marker);
            CameraPosition cameraPosition = new CameraPosition.Builder().target(new LatLng(list.get(i).getLatitudeServer()), list.get(i).getLongitudeServer())).zoom(15).build();

            mMap.animateCamera(CameraUpdateFactory
                    .newCameraPosition(cameraPosition));

        }

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

还有

     public class LocationDetail
 {
public double longitudeServer;
public double latitudeServer;
public String uniqueidSserver;

public double getLongitudeServer() {
    return longitudeServer;
}

public void setLongitudeServer(double longitudeServer) {
    this.longitudeServer = longitudeServer;
}

public double getLatitudeServer() {
    return latitudeServer;
}

public void setLatitudeServer(double latitudeServer) {
    this.latitudeServer = latitudeServer;
}

public String getUniqueidSserver() {
    return uniqueidSserver;
}

public void setUniqueidSserver(String uniqueidSserver) {
    this.uniqueidSserver = uniqueidSserver;
}

}

  

但是这行中的错误

CameraPosition cameraPosition = new CameraPosition.Builder()。target(new LatLng(list.get(i).getLatitudeServer()),list.get(i).getLongitudeServer()))。zoom(15).build( );它显示LatLnd Double Double无法应用于Double