【问题标题】:Android MapView Api - Change location markerAndroid MapView Api - 更改位置标记
【发布时间】:2017-03-26 03:48:55
【问题描述】:

我正在使用com.google.android.gms.maps.MapView,我想隐藏当前位置“标记”(下一张图片中的蓝色标记)

红色是我当前位置的标记,我想放置在我位置的中心,现在放置的位置就像一个向下的箭头。你知道我该怎么做吗?

编辑:我认为将我的标记放在蓝色标记上就足够了,它会覆盖它。我该怎么做?

我正在这样放置我的标记:

currentPositionMarker = googleMap.addMarker(new MarkerOptions().position(location).icon(BitmapDescriptorFactory.fromResource(R.drawable.pin)));

【问题讨论】:

    标签: android google-maps android-mapview currentlocation


    【解决方案1】:

    您可以设置标记图像的锚点:

    currentPositionMarker = 
        googleMap.addMarker(
            new MarkerOptions()
                .position(location)
                .anchor(0.5f, 0.5f)
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.pin)));
    

    你可以隐藏我的位置层在做什么

    googleMap.setMyLocationEnabled(false);
    

    【讨论】:

      【解决方案2】:

      您需要这样做: view_custom_marker.xml

       <?xml version="1.0" encoding="utf-8"?>
      <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/custom_marker_view"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:background="@drawable/marker_mask">
      
      <ImageView
          android:id="@+id/profile_image"
          android:layout_width="48dp"
          android:layout_height="48dp"
          android:layout_gravity="center_horizontal"
          android:contentDescription="@null"
          android:src="@drawable/avatar" />
       </FrameLayout>
      

      使用下面的代码将此视图转换为位图

          private Bitmap getMarkerBitmapFromView(@DrawableRes int resId) {
      
          View customMarkerView = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.view_custom_marker, null);
          ImageView markerImageView = (ImageView) customMarkerView.findViewById(R.id.profile_image);
          markerImageView.setImageResource(resId);
          customMarkerView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
          customMarkerView.layout(0, 0, customMarkerView.getMeasuredWidth(), customMarkerView.getMeasuredHeight());
          customMarkerView.buildDrawingCache();
          Bitmap returnedBitmap = Bitmap.createBitmap(customMarkerView.getMeasuredWidth(), customMarkerView.getMeasuredHeight(),
                  Bitmap.Config.ARGB_8888);
          Canvas canvas = new Canvas(returnedBitmap);
          canvas.drawColor(Color.WHITE, PorterDuff.Mode.SRC_IN);
          Drawable drawable = customMarkerView.getBackground();
          if (drawable != null)
              drawable.draw(canvas);
          customMarkerView.draw(canvas);
          return returnedBitmap;
      }
      }
      

      在地图就绪回调中添加您的自定义标记。

      @Override
      public void onMapReady(GoogleMap googleMap) {
      Log.d(TAG, "onMapReady() called with");
      mGoogleMap = googleMap;
      MapsInitializer.initialize(this);
      addCustomMarker();
      }
      private void addCustomMarker() {
      Log.d(TAG, "addCustomMarker()");
      if (mGoogleMap == null) {
          return;
      }
      
      // adding a marker on map with image from  drawable
      mGoogleMap.addMarker(new MarkerOptions()
      .position(mDummyLatLng).icon(BitmapDescriptorFactory.fromBitmap(getMarkerBitmapFromView(R.drawable.avatar))));
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-11-06
        • 2018-01-31
        • 1970-01-01
        • 2015-02-04
        • 2014-09-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多