【问题标题】:Replace default Android Maps API v2 Change MyLocation icon替换默认的 Android Maps API v2 更改 MyLocation 图标
【发布时间】:2013-01-27 09:26:07
【问题描述】:

我想用我自己的图像替换 Android Maps V2 用于“我的位置”的默认图标。我创建了自己的图块提供程序,它引入了一些主要是蓝色的地图,因此默认的“我的位置”图标,即蓝色的小箭头,很难看到。

以前我会重写 MyLocationOverlay 的 draw 方法,但新 API 中似乎没有。

我还需要图标能够旋转,就像箭头根据您面对的方向旋转一样。所以我不能只使用普通的标记。基本上我只需要为那个箭头创建一个自定义图像。

【问题讨论】:

  • 您是否在您的 sdk 中查看了 google map v2 的演示,该演示演示了使用自定义图像添加标记并相应地旋转它的功能。
  • 嘿 Grishu,我确实看过 SDK 中的示例,演示应用程序的哪个部分专门针对旋转我的位置图标?好像没找到。
  • 在 sdk 演示中检查标记的类别,其中显示了在地图上添加标记的方式。

标签: android google-maps android-maps-v2


【解决方案1】:

我的简单解决方法就是禁用谷歌地图的“我的位置”

并使用我的图标在地图上创建 ImageView,然后使用

捕获 ImageView

onClick 和 getMyLocation ,onClick 中的 animateCamera

 this.mGoogleMap.getUiSettings().setMyLocationButtonEnabled(false);
 this.mGoogleMap.setMyLocationEnabled(true);

.

@Override
public void onClick(final View v) {

    Location location = this.mGoogleMap.getMyLocation();

        if (location != null) {

            LatLng target = new LatLng(location.getLatitude(), location.getLongitude());
            CameraPosition position = this.mGoogleMap.getCameraPosition();

            Builder builder = new CameraPosition.Builder();
            builder.zoom(15);
            builder.target(target);

            this.mGoogleMap.animateCamera(CameraUpdateFactory.newCameraPosition(builder.build()));

          }    
}

【讨论】:

    【解决方案2】:

    Google Play 服务的最新更新现在允许您使用“平面”标记并旋转它们,这正是我所需要的。这是我实现它的方式的简单版本。我可能可以做一些事情来优化和调整动画,但它暂时可以完成这项工作。欢迎任何反馈。

    Marker mPositionMarker;
    GoogleMap mMap;
    
    @Override
    public void onLocationChanged(Location location) {
    
        if (location == null)
            return;
    
        if (mPositionMarker == null) {
    
            mPositionMarker = mMap.addMarker(new MarkerOptions()
                    .flat(true)
                    .icon(BitmapDescriptorFactory
                            .fromResource(R.drawable.positionIndicator))
                    .anchor(0.5f, 0.5f)
                    .position(
                            new LatLng(location.getLatitude(), location
                                    .getLongitude())));
        }
    
        animateMarker(mPositionMarker, location); // Helper method for smooth
                                                    // animation
    
        mMap.animateCamera(CameraUpdateFactory.newLatLng(new LatLng(location
                .getLatitude(), location.getLongitude())));
    
    }
    
    public void animateMarker(final Marker marker, final Location location) {
        final Handler handler = new Handler();
        final long start = SystemClock.uptimeMillis();
        final LatLng startLatLng = marker.getPosition();
        final double startRotation = marker.getRotation();
        final long duration = 500;
    
        final Interpolator interpolator = new LinearInterpolator();
    
        handler.post(new Runnable() {
            @Override
            public void run() {
                long elapsed = SystemClock.uptimeMillis() - start;
                float t = interpolator.getInterpolation((float) elapsed
                        / duration);
    
                double lng = t * location.getLongitude() + (1 - t)
                        * startLatLng.longitude;
                double lat = t * location.getLatitude() + (1 - t)
                        * startLatLng.latitude;
    
                float rotation = (float) (t * location.getBearing() + (1 - t)
                        * startRotation);
    
                marker.setPosition(new LatLng(lat, lng));
                marker.setRotation(rotation);
    
                if (t < 1.0) {
                    // Post again 16ms later.
                    handler.postDelayed(this, 16);
                }
            }
        });
    }
    

    【讨论】:

    • 这算轴承吗?
    • 为什么这行给我不兼容的类型错误? final 插值器插值器 = new LinearInterpolator();
    • 可以确认此动画方法不再有效。但是,如果您想平滑移动屏幕,只需使用 map.animateCamera() 而不是 map.moveCamera。现在对我来说已经足够了
    【解决方案3】:

    从 3.1.36 开始你不能改变图标(我觉得它会在未来改变,但看 API v2 改进实施的速度,它不会在圣诞节之前)。

    您现在可以使用标记,因为它具有setIcon 功能。

    基本上你必须忘记GoogleMap.setMyLocationEnabled,因为它总是会出现在上面。您将改为创建自己的LocationClient,并使用Location 中的纬度、经度和方位角来更新Marker。另外添加 Circle 并使用准确度来产生类似于默认 myLocation 可视化的效果。

    【讨论】:

    • 从 2019 年开始的问候,它从未被添加到 sdk,但从开发速度来看,在我未出生的孩子获得学士学位之前它还没有准备好
    【解决方案4】:

    您无法处理 Google 地图上 MyLocation 按钮的点击事件,是的,但是有一个技巧可以让您根据需要获得行为:
    首先在您的 layout.xml 文件中添加地图和虚拟 MyLocation 按钮。
    您可以借助相对布局来实现这一点:

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    
        <fragment
            android:id="@+id/map"
            android:name="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    
        <Button
            android:id="@+id/myLocationButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_marginRight="10dp"
            android:layout_marginTop="10dp"
            android:background="@drawable/mylocation" />
    </RelativeLayout>
    


    您可以使用“mylocation.png”,就像您在 Google 地图上看到的一样。
    LocationManager locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);

        Criteria criteria = new Criteria();
        String s = locationManager.getBestProvider(criteria, false);
    
        Location location = locationManager.getLastKnownLocation(s);  
    

    调用虚拟 MyLocation 按钮的单击事件,您将在其中添加标记
    private GoogleMap googleMap; googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap(); LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude()); googleMap.addMarker(new MarkerOptions().icon(YOUR_MODIFIED_ICON).position(latLng).title(TITLE).snippet(SNIPET));
    这样就可以修改谷歌地图的默认图标了。
    是的,您还需要设置缩放级别:
    googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

    【讨论】:

      【解决方案5】:

      这是访问位置按钮的代码:

      View mv=findViewById(R.id.map);
              View locationButton = ((View) mv.findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("2"));
              RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) locationButton.getLayoutParams();
      // position on right bottom
              rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
              rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
              rlp.setMargins(0, 180, 180, 0);
      

      【讨论】:

      • 欢迎来到 Stack Overflow!感谢您提供此代码 sn-p,它可能会提供一些即时帮助。一个正确的解释would greatly improve 其教育价值通过展示为什么这是一个很好的解决问题的方法,并将使它对未来有类似但不相同的问题的读者更有用。请edit您的答案添加解释,并说明适用的限制和假设。
      【解决方案6】:

      试试这个;我使用它在地图上绘制路线,但我为标记和我的位置设置图标 声明这一点以查看您的位置:

          LocationManager locationManager = (LocationManager) 
          getSystemService(LOCATION_SERVICE);
          Criteria criteria = new Criteria();
          String provider = locationManager.getBestProvider(criteria, true);
          Location myLocation= locationManager.getLastKnownLocation(provider);
      
          longitude = myLocation.getLongitude();
          latitude  = myLocation.getLatitude();
          fromPosition = new LatLng(latitude, longitude);
      

      并创建一个类似的函数,我用于在地图上绘制,但我在标记和我的位置上设置了图标:

      mGoogleMap.clear();
      if(response.equalsIgnoreCase("Success"))
      {
           ArrayList<LatLng> directionPoint = v2GetRouteDirection.getDirection(document);
           PolylineOptions rectLine = new PolylineOptions().width(7).color(Color.BLUE);
      
           for (int i = 0; i < directionPoint.size(); i++) {
           rectLine.add(directionPoint.get(i));
           }
           // Adding route on the map
           mGoogleMap.setOnMarkerClickListener(MainActivity.this);
           mGoogleMap.addPolyline(rectLine);
           markerOptions.position(fromPosition);
           markerOptions.draggable(true);
           Marker1 = mGoogleMap.addMarker(new MarkerOptions()
                     .position(Estadio)
                     .title("Estadio Cuscatlan")
                     .snippet("Estadio Cuscatlan")                                                    
                     .icon(BitmapDescriptorFactory                                          
                     .fromResource(R.drawable.mapmarker))); 
           Marker2 = mGoogleMap.addMarker(new MarkerOptions()
                     .position(Metrocentro)
                 .title("Metrocentro")
                 .snippet("Metrosuelo")
                 .icon(BitmapDescriptorFactory
                 .fromResource(R.drawable.mapmark
            Marker3 = mGoogleMap.addMarker(new MarkerOptions()
                  .position(Mejicanos)
                  .title("Mejicanos")
                  .snippet("Mejicanos")
                  .icon(BitmapDescriptorFactory
                  .fromResource(R.drawable.mapmarker)));  
            MarkerMe = mGoogleMap.addMarker(new MarkerOptions()                                             
                              .position(fromPosition) 
                              .icon(BitmapDescriptorFactory
                              .fromResource(R.drawable.car64)));
         }
          Dialog.dismiss();
          } 
      } 
      

      【讨论】:

      • 你能在图片上做标题吗.title("Estadio Cuscatlan")=set in image address of title set in image can you make it........
      猜你喜欢
      • 1970-01-01
      • 2013-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-29
      • 1970-01-01
      相关资源
      最近更新 更多