google map api v2 add icon from url android

偶尔善良 提交于 2019-12-07 12:06:52

问题


I have a project that retrieves latitude and longitude from web service and I add them to google map markers. I want to add custom icon for the markers from url.

    mMap.addMarker(new MarkerOptions()
                    .position(new LatLng(dlat,Dlong))
                    .title(m.group(9))
                    .draggable(false)                                           
                    .snippet(m.group(1)+" "+m.group(10)));

How I can add .icon with a custom icon from a link?


回答1:


Use this method and use returned bitmap to display in map





public Bitmap getBitmapFromURL(String imageUrl) {
    try {
        URL url = new URL(imageUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}



回答2:


try{
                        URL url = new URL(Lurl);
                        /** Creating an http connection to communcate with url */
                        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                        /** Connecting to url */
                        urlConnection.connect();
                        /** Reading data from url */
                        iStream = urlConnection.getInputStream();
                        /** Creating a bitmap from the stream returned from the url */
                        bitmap = BitmapFactory.decodeStream(iStream);

                    }catch(Exception e){
                        Log.d("Exception while downloading url", e.toString());
                    }finally{
                        iStream.close();
                    }

this worked for me



来源:https://stackoverflow.com/questions/23732712/google-map-api-v2-add-icon-from-url-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!