【问题标题】:android how to draw a circle on google mapandroid如何在谷歌地图上画一个圆圈
【发布时间】:2015-07-09 11:18:24
【问题描述】:

我正在为我的 android 应用程序使用新的 API(Google Map API V2),我已经创建了地图并为其添加了标记,现在我的任务是手动围绕任何标记创建一个圆圈,我也想要为用户提供一个功能,他可以相应地增加该圆的半径,为此我给出了一个条,当用户增加该条时,圆的半径将增加,反之亦然。

如果有人知道如何使用 Google Map API V2 做到这一点,请帮助,

【问题讨论】:

    标签: android


    【解决方案1】:

    试试这个代码

    protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_tag_map);
    
         // Drawing circle on the map
               drawCircle(new LatLng(Latitude, Longitude));
        }
    
            private void drawCircle(LatLng point){
    
                    // Instantiating CircleOptions to draw a circle around the marker
                    CircleOptions circleOptions = new CircleOptions();
    
                    // Specifying the center of the circle
                    circleOptions.center(point);
    
                    // Radius of the circle
                    circleOptions.radius(20);
    
                    // Border color of the circle
                    circleOptions.strokeColor(Color.BLACK);
    
                    // Fill color of the circle
                    circleOptions.fillColor(0x30ff0000);
    
                    // Border width of the circle
                    circleOptions.strokeWidth(2);
    
                    // Adding the circle to the GoogleMap
                    googleMap.addCircle(circleOptions);
    
                }
    

    【讨论】:

    • 是的,很好,但我的任务是从搜索栏管理标记圈
    • public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { // TODO 自动生成的方法 stub pro=progress; googleMap.addCircle(new CircleOptions() .center(new LatLng(point.latitude,point.longitude)) .radius(pro) .strokeWidth(0f) .fillColor(0x550000FF) );
    • 当用户在地图上移动时,如何使圆圈保持在地图的中心?
    【解决方案2】:

    onMapReady()方法中添加如下代码:

    Circle circle = map.addCircle(new CircleOptions()
         .center(new LatLng(latitude, longitude))
         .radius(10000)
         .strokeColor(Color.RED)
         .fillColor(Color.BLUE));
    

    其中mapGoogleMap 类的对象。

    【讨论】:

      【解决方案3】:

      Google 地图可以为圆圈使用半透明颜色。

      private var circle: Circle? = null
      
      private fun drawCircle(latitude: Double, longitude: Double, radius: Double) {
          val circleOptions = CircleOptions()
              .center(LatLng(latitude, longitude))
              .radius(radius)
              .strokeWidth(1.0f)
              .strokeColor(ContextCompat.getColor(context!!, R.color.color1))
              .fillColor(ContextCompat.getColor(context!!, R.color.color2))
          circle?.remove() // Remove old circle.
          circle = googleMap?.addCircle(circleOptions) // Draw new circle.
      }
      

      【讨论】:

        【解决方案4】:
                     CircleOptions circleOptions = new CircleOptions();
                        circleOptions.center(new LatLng(location.getLatitude(),location.getLongitude()));
                        circleOptions.radius(700);
                        circleOptions.fillColor(Color.TRANSPARENT);
                        circleOptions.strokeWidth(6);
                        mMap.addCircle(circleOptions);
                    geocoder = new Geocoder(MapsActivity.this, getDefault());
                    MarkerOptions markerOptions = new MarkerOptions();
                    markerOptions.position(new LatLng(location.getLatitude(), location.getLongitude()));
                    mMap.addMarker(markerOptions.title(String.valueOf(location)));
        

        试试这个

        【讨论】:

        • 虽然此代码 sn-p 可能是解决方案,但 including an explanation 确实有助于提高您的帖子质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因。
        【解决方案5】:

        @Lalit Kushwah 的 Kotlin 版本

        map.addCircle(
            CircleOptions()
                .center([Your LatLng])
                .radius(10000.0)
                .strokeColor(Color.RED)
                .fillColor(Color.BLUE)
        )
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-11-11
          • 2016-07-30
          • 2013-02-12
          • 1970-01-01
          • 1970-01-01
          • 2011-04-15
          • 2018-06-24
          • 1970-01-01
          相关资源
          最近更新 更多