如何为每个标记设置不同的图标?

时间:2016-04-02 09:18:51

标签: android android-layout listview android-fragments

如何为每个标记设置不同的图标,从服务器接收标记经度和纬度,并在地图屏幕上绘制每个设备的逻辑和纬度,并每隔5秒删除之前的标记。

 try {
            //ArrayList list = new ArrayList<>();
            //TODO: remove previus markers
            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.getId()+"  Marker id.......");
        } catch (Exception e) {
            Toast.makeText(MainActivity.this, "Error ", Toast.LENGTH_LONG).show();
            // mMap.clear();
        }

1 个答案:

答案 0 :(得分:1)

您需要在服务器上存储标记图像和位置(纬度和经度)。在从服务器检索数据时,您将获得两者。以下是示例代码:

if(googleMap != null){
        googleMap.clear(); // This will clear all of the previously added markers
        ArrayList<ServerItems> listOfItems = new ArrayList<>(); // Where ServerItems is your custom Model
        for(ServerItems item : listOfItems){
            LatLng position = item.getPosition(); // Assuming that Your Model ServerItems contains a method which returns LatLng
            String markerURL = item.getMarkerURL(); // Assuming that Your Model ServerItems contains a method which returns MarkerURL stored on Server
            ImageLoader.getInstance().loadImage(markerURL, new ImageLoadingListener() {
                @Override
                public void onLoadingStarted(String imageUri, View view) {

                }

                @Override
                public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                    googleMap.addMarker(new MarkerOptions().position(position));
                }

                @Override
                public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                    googleMap.addMarker(new MarkerOptions().position(position).icon(BitmapDescriptorFactory.fromBitmap(loadedImage)));
                }

                @Override
                public void onLoadingCancelled(String imageUri, View view) {
                    googleMap.addMarker(new MarkerOptions().position(position));
                }
            });

        }

    }

请注意,我正在使用UniversalImageLoader,以便从服务器以位图的形式加载图片。