简体   繁体   English

从 URL 在 Google Maps v2 Android 上设置标记图标

[英]Set marker icon on Google Maps v2 Android from URL

I am unable to add the icon provided by the Google Places API to the marker.我无法将 Google Places API 提供的图标添加到标记中。 For example, I would like to have the icon returned from the JSON of Google Places API:例如,我希望从 Google Places API 的 JSON 返回图标:

"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png"

Here's how I am doing it now without adding the icon:这是我现在不添加图标的方式:

for (Place place : mNearPlaces.results) {
     LatLng placeLatLng = new LatLng(place.geometry.location.lat,
         map.addMarker(new MarkerOptions()
             .position(placeLatLng)
             .title(place.name));
}

I looked at this post and the answer suggested using some AsyncTask to download the image.我看了这篇文章,答案建议使用一些 AsyncTask 来下载图像。 Is there any other way to do this (that way seems slow)?有没有其他方法可以做到这一点(这种方式看起来很慢)? Is there a standard way of setting the marker image provided by the Google Places API?是否有设置 Google Places API 提供的标记图像的标准方法?

Here is theGoogle Maps Android API v2 Documentation for Markers这是标记Google Maps Android API v2 文档

Thanks谢谢

Hi use a librariy that help you to view images from URL.嗨,使用可帮助您从 URL 查看图像的库。 visit https://github.com/nostra13/Android-Universal-Image-Loader for Android-Universal-Image-Loader访问https://github.com/nostra13/Android-Universal-Image-Loader for Android-Universal-Image-Loader

also you can visit to know for downloading the image您也可以访问以了解下载图像

http://brainflush.wordpress.com/2009/11/23/droid-fu-part-2-webimageview-and-webgalleryadapter/ http://brainflush.wordpress.com/2009/11/23/droid-fu-part-2-webimageview-and-webgalleryadapter/

and after that在那之后

for the Custom Marker Icon, you can use an icon to show as a marker.对于自定义标记图标,您可以使用图标显示为标记。 load the icon from any kind of supported sources.从任何类型的受支持来源加载图标。

fromAsset(String assetName) – Loading from assets folder fromAsset(String assetName) – 从资产文件夹加载

fromBitmap (Bitmap image) – Loading bitmap image fromBitmap (Bitmap image) – 加载位图图像

fromFile (String path) – Loading from file fromFile (String path) – 从文件加载

fromResource (int resourceId) – Loading from drawable resource fromResource (int resourceId) – 从可绘制资源加载

try as follows尝试如下

// latitude and longitude
double latitude = 17.385044;
double longitude = 78.486671;

// create marker
MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Hello Maps");

// Changing marker icon
// set yours icon here
marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.my_marker_icon)));

// adding marker
googleMap.addMarker(marker);

You can load url image to a GoogleMap mapMarker using following code.您可以使用以下代码将 url 图像加载到 GoogleMap mapMarker。 First set other properties like title, snippet, position etc....首先设置其他属性,如标题、片段、位置等....

  mapMarker= mGoogleMap.addMarker(new MarkerOptions()
                    .title(Integer.toString(total_steps))
                    .snippet("Steps")
                    .position(new LatLng(current_lat,current_longi)));
            mapMarker.setTag("current_position");
            mapMarker.showInfoWindow();
            loadMarkerIcon(mapMarker);

After use a custom method to load a image for icon property.使用自定义方法为图标属性加载图像后。 You can add Glide gradle link to project.您可以将 Glide gradle 链接添加到项目。

private void loadMarkerIcon(final Marker marker) {
        String burlImg = "Url_imagePath;
        Glide.with(this).load(burlImg)
                .asBitmap().fitCenter().into(new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(Bitmap bitmap, GlideAnimation<? super Bitmap> glideAnimation) {

                if(bitmap!=null){
                  //  Bitmap circularBitmap = getRoundedCornerBitmap(bitmap, 150);
                    Bitmap mBitmap = getCircularBitmap(bitmap);
                    mBitmap = addBorderToCircularBitmap(mBitmap, 2, Color.WHITE,squareBitmapWidth);
                    BitmapDescriptor icon = BitmapDescriptorFactory.fromBitmap(mBitmap);
                    marker.setIcon(icon);
                }

            }
        });

    }

there are a lot of libraries that help you view images from URL like: URLImageViewhelper unfortunately, you can't load images from URL without AsyncTask however all the libraries that help you view images from URL are using Asynctask so only you have to do is call the function.有很多库可以帮助您从 URL 查看图像,例如: URLImageViewhelper不幸的是,如果没有AsyncTask ,您无法从 URL 加载图像,但是所有帮助您从 URL 查看图像的库都使用Asynctask因此您只需调用功能。

the implementation of the library class are available within the github page.库类的实现在 github 页面中可用。

While calling .icon() do the following在调用 .icon() 时执行以下操作

 protected Marker createMarker(double latitude, double longitude, String title, String snippet, String imageUrl) {

    MarkerOptions markerOptions = new MarkerOptions();
    return googleMap.addMarker(markerOptions
            .position(new LatLng(latitude, longitude))
            .anchor(0.5f, 0.5f)
            .title(title)
            .snippet(snippet)
            .icon(BitmapDescriptorFactory.fromBitmap(getBitmapFromLink(imageUrl))));
}

This method will convert the image url to bitmap此方法会将图像 url 转换为位图

   public Bitmap getBitmapFromLink(String link) {
    try {
        URL url = new URL(link);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        try {
            connection.connect();
        } catch (Exception e) {
            Log.v("asfwqeds", e.getMessage());
        }
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
    } catch (IOException e) {
        Log.v("asfwqeds", e.getMessage());
        e.printStackTrace();
        return null;
    }
}

Now, an exception will occur in connection.connect() saying android.os.NetworkOnMainThreadException.现在,connection.connect() 中会发生一个异常,表示 android.os.NetworkOnMainThreadException。 To handle this Exception add this code in you oncreate or before adding the marker要处理此异常,请在您创建时或添加标记之前添加此代码

if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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