Android Cluster Manager图标取决于类型

Android Cluster Manager图标取决于类型,第1张

概述我正在开发一个小应用程序并实施了Google Maps和Places api.目前我能够在地图上看到我的所有标记,并且群集工作正常.我可以放大群集打开并能够看到标记.我有一个具有不同类型的微调器,一旦选择该类型传递给位置搜索字符串. 这是我的地图代码,包括群集: public class MapsActivity extends FragmentActivity implements Locati 我正在开发一个小应用程序并实施了Google Maps和Places API.目前我能够在地图上看到我的所有标记,并且群集工作正常.我可以放大群集打开并能够看到标记.我有一个具有不同类型的微调器,一旦选择该类型传递给位置搜索字符串.

这是我的地图代码,包括群集:

public class MapsActivity extends FragmentActivity implements LocationListener,ClusterManager.OnClusterItemInfoWindowClickListener<MyItem> {    GoogleMap mMap;    double myLatitude = 0;    double myLongitude = 0;    HashMap<String,String> mMarker = new HashMap<String,String>();    PlaceJsONParser placeJsonParser = new PlaceJsONParser();    private ClusterManager<MyItem> mClusterManager;    protected MyItem clickedClusterItem;    String[] placeType;    String[] placeTypename;    Spinner spinPlaceType;    @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(R.ID.map);        mMap = mapFragment.getMap();        onMapReady();        // Array of place types        placeType = getResources().getStringArray(R.array.placeType);        // Array of place type names        placeTypename = getResources().getStringArray(R.array.placeTypename);        // Creating an array adapter with an array of Place types        // to populate the spinner        ArrayAdapter<String> adapter = new ArrayAdapter<>(this,R.layout.spinner_item,R.ID.textvIEw,placeTypename);        // Getting reference to the Spinner        spinPlaceType = (Spinner) findVIEwByID(R.ID.spinPlaceType);        // Setting adapter on Spinner to set place types        spinPlaceType.setAdapter(adapter);        spinPlaceType.setonItemSelectedListener(new AdapterVIEw.OnItemSelectedListener() {            @OverrIDe            public voID onItemSelected(AdapterVIEw<?> parent,VIEw vIEw,int position,long ID) {                int selectedposition = spinPlaceType.getSelectedItemposition();                final String type = placeType[selectedposition];                StringBuilder sb = new StringBuilder(                        "https://maps.GoogleAPIs.com/maps/API/place/nearbysearch/Json?");                sb.append("location=" + myLatitude + "," + myLongitude);                sb.append("&type=" + type);                sb.append("&radius=4000");                sb.append("&key=PLACES_KEY");                // Creating a new non-ui thread task to download Google place Json                // data                PlacesTask placesTask = new PlacesTask();                // Invokes the "doInBackground()" method of the class PlaceTask                placesTask.execute(sb.toString());            }            @OverrIDe            public voID onnothingSelected(AdapterVIEw<?> parent) {                StringBuilder sb = new StringBuilder(                        "https://maps.GoogleAPIs.com/maps/API/place/nearbysearch/Json?");                sb.append("location=" + myLatitude + "," + myLongitude);                sb.append("&type=restaurant");                sb.append("&radius=4000");                sb.append("&key=PLACES_KEY");                // Creating a new non-ui thread task to download Google place Json                // data                PlacesTask placesTask = new PlacesTask();                // Invokes the "doInBackground()" method of the class PlaceTask                placesTask.execute(sb.toString());            }        });        // Will display next 20 places returned form the next_page_token        floatingActionbutton fab = (floatingActionbutton) findVIEwByID(R.ID.fab_more);        fab.setonClickListener(new VIEw.OnClickListener() {            @OverrIDe            public voID onClick(VIEw vIEw) {                Snackbar.make(vIEw,"Finding you some more places.",Snackbar.LENGTH_LONG)                        .setAction("Action",null).show();                StringBuilder sb = new StringBuilder(                        "https://maps.GoogleAPIs.com/maps/API/place/nearbysearch/Json?");                sb.append("pagetoken=" + placeJsonParser.getNext_Page_token());                sb.append("&key=PLACES_KEY");                // Creating a new non-ui thread task to download Google place Json                // data                if (placeJsonParser.getNext_Page_token() == null || placeJsonParser.getNext_Page_token() == ""){                    Snackbar.make(vIEw,"No more places left to find.",Snackbar.LENGTH_SHORT)                            .setAction("Action",null).show();                }                PlacesTask placesTask = new PlacesTask();                // Invokes the "doInBackground()" method of the class PlaceTask                placesTask.execute(sb.toString());            }        });        mMap.setonInfoWindowClickListener(new OnInfoWindowClickListener() {            @OverrIDe            public voID onInfoWindowClick(Marker marker) {                Intent detailsIntent = new Intent(getBaseContext(),PlaceDetailsActivity.class);                String reference = mMarker.get(marker.getID());                marker.getposition();                detailsIntent.putExtra("reference",reference);                detailsIntent.putExtra("markerLat",myLatitude);                detailsIntent.putExtra("markerLong",myLongitude);                startActivity(detailsIntent);            }        });    }    public voID onMapReady(){        // Enabling MyLocation in Google Map        mMap.setMyLocationEnabled(true);        mMap.getUiSettings().setCompassEnabled(true);        mMap.getUiSettings().setZoomControlsEnabled(true);        // Getting LocationManager object from System Service        // LOCATION_SERVICE        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);        // Creating a criteria object to retrIEve provIDer        Criteria criteria = new Criteria();        // Getting the name of the best provIDer        String provIDer = locationManager.getBestProvIDer(criteria,true);        // Getting Current Location From GPS        Location location = locationManager.getLastKNownLocation(provIDer);        // onLocationChanged(location);        if (location != null) {            onLocationChanged(location);        }    }    /**     * A method to download Json data from url     */    private String downloadUrl(String strUrl) throws IOException {        String referer ="";        StringBuilder JsonResults = new StringBuilder();        httpURLConnection conn = null;        try {            URL url = new URL(strUrl);            // Creating an http connection to communicate with url            conn = (httpURLConnection) url.openConnection();            if (referer != null) {                conn.setRequestProperty("Referer",referer);            }            inputStreamReader in = new inputStreamReader(conn.getinputStream());            // Load the results into a StringBuilder            int read;            char[] buff = new char[1024];            while ((read = in.read(buff)) != -1) {                JsonResults.append(buff,read);            }            // displays the List of places found in the terminal.            Log.i("Data","Places Found: " + JsonResults);        } catch (MalformedURLException e) {            Log.i("Google Places Utility","Error processing Places API URL");            return null;        } catch (IOException e) {            Log.i("Google Places Utility","Error connecting to Places API");            return null;        } finally {            if (conn != null) {                conn.disconnect();            }        }        return JsonResults.toString();    }    /**     * A class,to download Google Places     */    private class PlacesTask extends AsyncTask<String,Integer,String> {        String data = null;        // Invoked by execute() method of this object        @OverrIDe        protected String doInBackground(String... url) {            try {                data = downloadUrl(url[0]);            } catch (Exception e) {                Log.d("Background Task",e.toString());            }            return data;        }        // Executed after the complete execution of doInBackground() method        @OverrIDe        protected voID onPostExecute(String result) {            ParserTask parserTask = new ParserTask();            // Start parsing the Google places in JsON format            // Invokes the "doInBackground()" method of the class ParseTask            parserTask.execute(result);        }    }    /**     * A class to parse the Google Places in JsON format     */    private class ParserTask extends            AsyncTask<String,List<HashMap<String,String>>> {        JsONObject jObject;        // Invoked by execute() method of this object        @OverrIDe        protected List<HashMap<String,String>> doInBackground(                String... JsonData) {            List<HashMap<String,String>> places = null;            try {                jObject = new JsONObject(JsonData[0]);                /** Getting the parsed data as a List construct */                places = placeJsonParser.parse(jObject);            } catch (Exception e) {                Log.d("Exception",e.toString());            }            return places;        }        // Executed after the complete execution of doInBackground() method        @OverrIDe        protected voID onPostExecute(List<HashMap<String,String>> List) {            // Clears all the existing markers            mMap.clear();            setUpClusterer(List);        }    }    private voID setUpClusterer(List<HashMap<String,String>> List) {        // position the map.        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(myLatitude,myLongitude),13));        // Initialize the manager with the context and the map.        // (Activity extends context,so we can pass 'this' in the constructor.)        mClusterManager = new ClusterManager<MyItem>(this,mMap);        // Point the map's Listeners at the Listeners implemented by the cluster        // manager.        mMap.setonCamerachangelistener(mClusterManager);        mMap.setonMarkerClickListener(mClusterManager);        mMap.setInfoWindowAdapter(mClusterManager.getMarkerManager());        mMap.setonInfoWindowClickListener(mClusterManager);        mClusterManager.setonClusterItemInfoWindowClickListener(this);        mClusterManager                .setonClusterItemClickListener(new ClusterManager.OnClusterItemClickListener<MyItem>() {                    @OverrIDe                    public boolean onClusterItemClick(MyItem item) {                        clickedClusterItem = item;                        return false;                    }                });        // Add cluster items (markers) to the cluster manager.        addItems(List);        mClusterManager.getMarkerCollection().setonInfoWindowAdapter(                new MyCustomAdapterForItems());    }    public class MyCustomAdapterForItems implements GoogleMap.InfoWindowAdapter {        private final VIEw myContentsVIEw;        MyCustomAdapterForItems() {            myContentsVIEw = getLayoutInflater().inflate(                    R.layout.info_window,null);        }        @OverrIDe        public VIEw getInfoWindow(Marker marker) {            TextVIEw tvTitle = ((TextVIEw) myContentsVIEw                    .findVIEwByID(R.ID.txtTitle));            TextVIEw tvSnippet = ((TextVIEw) myContentsVIEw                    .findVIEwByID(R.ID.txtSnippet));            tvTitle.setText(clickedClusterItem.getTitle());            tvSnippet.setText(clickedClusterItem.getSnippet());            return myContentsVIEw;        }        @OverrIDe        public VIEw getInfoContents(Marker marker) {            return null;        }    }    private voID addItems(List<HashMap<String,String>> List) {        double latitude;        double longitude;        for (int i = 0; i < List.size(); i++) {            HashMap<String,String> hmPlace = List.get(i);            // Getting latitude of the place            latitude = Double.parseDouble(hmPlace.get("lat"));            // Getting longitude of the place            longitude = Double.parseDouble(hmPlace.get("lng"));            String name = hmPlace.get("place_name");            // Getting vicinity            String vicinity = hmPlace.get("vicinity");            MyItem offsetItem = new MyItem(latitude,longitude,hmPlace.get("reference"),name,vicinity);            mClusterManager.addItem(offsetItem);        }    }    public voID onClusterItemInfoWindowClick(MyItem item) {        Intent placesIntent = new Intent(getBaseContext(),PlaceDetailsActivity.class);        String reference = item.getReference();        placesIntent.putExtra("name",item.getTitle());        placesIntent.putExtra("reference",reference);        placesIntent.putExtra("sourcelat",myLatitude);        placesIntent.putExtra("sourcelng",myLongitude);        startActivity(placesIntent);    }    @OverrIDe    public voID onLocationChanged(Location location) {        myLatitude = location.getLatitude();        myLongitude = location.getLongitude();        LatLng myLocation = new LatLng(myLatitude,myLongitude);        mMap.moveCamera(CameraUpdateFactory.newLatLng(myLocation));        mMap.animateCamera(CameraUpdateFactory.zoomTo(13));    }    @OverrIDe    public voID onStatusChanged(String provIDer,int status,Bundle extras) {    }    @OverrIDe    public voID onProvIDerEnabled(String provIDer) {    }    @OverrIDe    public voID onProvIDerDisabled(String provIDer) {    }}

我的myItem类获取标记的信息:

package com.example.tariq.outandabout;import com.Google.androID.gms.maps.model.LatLng;import com.Google.maps.androID.clustering.ClusterItem;public class MyItem implements ClusterItem {    LatLng mposition;    private String reference,placeTitle,snippet;    public MyItem(double lat,double lng,String val,String Title,String snip) {        mposition = new LatLng(lat,lng);        reference=val;        placeTitle=Title;        snippet = snip;    }    @OverrIDe    public LatLng getposition() {        // Todo auto-generated method stub        return mposition;    }    public String getReference() {        // Todo auto-generated method stub        return reference;    }    public String getTitle() {        // Todo auto-generated method stub        return placeTitle;    }    public String getSnippet() {        // Todo auto-generated method stub        return snippet;    }}

目前只显示红色标记,但我想知道是否有一种方法可以根据从微调器中选择的类型使用不同的标记,例如,如果我选择医院,那么标记显示为小医院图标,如果我选择ATM,然后出现一个小ATM图标.

任何帮助将不胜感激.

解决方法 首先,您必须存储所需的所有信息(在这种情况下只需LatLng和标记图标)以确保ClusterItem对象.

public class MarkerItem implements ClusterItem {private String Title;private String snippet;private LatLng latLng;private BitmapDescriptor icon;public MarkerItem(MarkerOptions markerOptions) {    this.latLng = markerOptions.getposition();    this.Title = markerOptions.getTitle();    this.snippet = markerOptions.getSnippet();    this.icon = markerOptions.getIcon();}@OverrIDepublic LatLng getposition() {    return latLng;}public String getTitle() {    return Title;}public String getSnippet() {    return snippet;}public voID setLatLng(LatLng latLng) {    this.latLng = latLng;}public BitmapDescriptor getIcon() {    return icon;}public voID setIcon(BitmapDescriptor icon) {    this.icon = icon;}}

下一步是使群集渲染器显示您的图标而不是默认制造商图标.要实现这一点,您需要扩展DefaultClusterRenderer对象:

public class ClusterRenderer extends DefaultClusterRenderer<MarkerItem> {public ClusterRenderer(Context context,GoogleMap map,ClusterManager<MarkerItem> clusterManager) {    super(context,map,clusterManager);    clusterManager.setRenderer(this);}@OverrIDeprotected voID onBeforeClusterItemRendered(MarkerItem markerItem,MarkerOptions markerOptions) {    if (markerItem.getIcon() != null) {        markerOptions.icon(markerItem.getIcon()); //Here you retrIEve BitmapDescriptor from ClusterItem and set it as marker icon    }    markerOptions.visible(true);}}

最后,您必须初始化clusterRenderer和markersItems

ClusterManager clusterManager = new ClusterManager<>(context,GoogleMap);ClusterRenderer clusterRenderer = new ClusterRenderer<>(activity,GoogleMap,clusterManager); // not needed to use clusterManager.setRenderer method since i made it in constructorMarkerOptions markerOptions = new MarkerOptions()            .position(new LatLng(latitude,longitude))            .icon(BitmapDescriptorFactory.fromresource(R.drawable.your_resource_icon));MarkerItem markerItem = new MarkerItem(markerOptions);clusterManager.addItem(markerItem);

您可以实现自己的逻辑,这个图标要传递给markerItem.

编辑
要传递不同的图标,您可以为其创建单独的方法
例:

public MarkerOptions getMarkerOptions(LatLng latLng,String snippet,int iconRes) {    return new MarkerOptions()            .Title(Title)            .snippet(snippet)            .position(latLng)            .icon(BitmapDescriptorFactory.fromresource(iconRes));}

编辑2我更新了MarkerItem类以满足您的需求,用MarkerItem类替换您的MyItem类.使用此课程添加项目并更新以满足您的需求

总结

以上是内存溢出为你收集整理的Android Cluster Manager图标取决于类型全部内容,希望文章能够帮你解决Android Cluster Manager图标取决于类型所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1122473.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存