【问题标题】:Set marker icon on Google Maps v2 Android from URL通过 URL 在 Google Maps v2 Android 上设置标记图标
【发布时间】:2014-03-03 05:45:17
【问题描述】:

我无法将 Google Places API 提供的图标添加到标记中。例如,我希望从 Google Places API 的 JSON 返回图标:

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

这是我现在在不添加图标的情况下的做法:

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

我查看了this post,答案建议使用一些 AsyncTask 下载图像。有没有其他方法可以做到这一点(这种方式似乎很慢)?是否有设置 Google Places API 提供的标记图像的标准方法?

这里是Google Maps Android API v2 Documentation for Markers

谢谢

【问题讨论】:

    标签: android google-maps google-maps-markers google-places-api


    【解决方案1】:

    您可以使用以下代码将 url 图片加载到 GoogleMap mapMarker。首先设置其他属性,如标题、sn-p、位置等......

      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);
    

    使用自定义方法为图标属性加载图像后。您可以将 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);
                    }
    
                }
            });
    
        }
    

    【讨论】:

      【解决方案2】:

      您好,请使用可帮助您查看来自 URL 的图像的库。 Android-Universal-Image-Loader

      访问 https://github.com/nostra13/Android-Universal-Image-Loader

      你也可以访问了解下载图片

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

      然后

      对于自定义标记图标,您可以使用图标作为标记显示。从任何受支持的来源加载图标。

      fromAsset(String assetsName) – 从 assets 文件夹加载

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

      fromFile (String path) – 从文件加载

      fromResource (int resourceId) – 从可绘制资源加载

      如下尝试

      // 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);
      

      【讨论】:

        【解决方案3】:

        有很多库可以帮助您从 URL 中查看图像,例如:URLImageViewhelper 不幸的是,如果没有 AsyncTask,您将无法从 URL 加载图像,但是所有帮助您从 URL 中查看图像的库都在使用 @987654323 @ 所以只有你需要做的就是调用函数。

        库类的实现可在 github 页面中找到。

        【讨论】:

          【解决方案4】:

          在调用 .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))));
          }
          

          此方法会将图片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;
              }
          }
          

          现在,connection.connect() 中会发生异常,说 android.os.NetworkOnMainThreadException。要处理此异常,请在您的 oncreate 中或添加标记之前添加此代码

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-03-30
            • 2014-01-28
            • 2013-06-14
            • 2023-03-14
            • 2013-08-31
            • 1970-01-01
            • 2013-03-22
            • 1970-01-01
            相关资源
            最近更新 更多