如何在圈子中绑定谷歌地图标记?

时间:2016-04-17 19:41:54

标签: java android google-maps google-maps-markers google-maps-android-api-2

我需要在谷歌地图圈中绑定多个标记。我正在调用onMapReady中的跟踪方法来加载标记:

void addMarker()
{
 mMap.clear();
    for (int i = 0; i < response.getData().getCount(); i++) {
        float lat = response.getData().get(i).getLat();
        float lng = response.getData().get(i).getLng();
........
........

 LatLng location = new LatLng(lat, lng);
 Marker mapMarker = mMap.addMarker(new MarkerOptions()
        .position(location)
         .draggable(false)
         .icon(bitmapMarker));
  mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(location, 17));
}

@Override
public void onMapReady(GoogleMap googleMap) {
   mMap = googleMap;
   addMarkers();
   DraggableCircle circle = new DraggableCircle(LOCATION, DEFAULT_RADIUS); 
}

根据上面的代码,我可以在给定位置绘制circle并在给定的coordinates上添加标记。一切都很好,如果我增加圆的大小,那么地图将根据屏幕尺寸调整缩放尺寸。

现在我在找什么:

  1. 将在屏幕上显示标记,该标记符合圆直径(位置和半径)
  2. 一旦用户增加radius新标记就会出现。
  3. 任何提示或帮助都会很明显。

1 个答案:

答案 0 :(得分:2)

我希望我没有误解你。

在底部有一个搜索栏,每当你改变它时,也会改变圆的半径。

然后我按照filterMarkers方法中的新半径值过滤标记

enter image description here enter image description here enter image description here enter image description here

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;
    private SeekBar mRadiusSeekBar;

    private Circle mCircle;
    private double mCircleRadius = 250;
    private LatLng mCircleCenter = new LatLng(38.432398, 27.155882);

    private void addCircle(){
        mCircle = mMap.addCircle(new CircleOptions()
                .strokeWidth(4)
                .radius(mCircleRadius)
                .center(mCircleCenter)
                .strokeColor(Color.parseColor("#D1C4E9"))
                .fillColor(Color.parseColor("#657C4DFF")));
    }

    private List<LatLng> mPoints = new ArrayList<>();
    private List<Marker> mMarkers = new ArrayList<>();

    private void addMarkers(){
        LatLng mPoint1 = new LatLng(38.440925, 27.153672);
        LatLng mPoint2 = new LatLng(38.440700, 27.155877);
        LatLng mPoint3 = new LatLng(38.438402, 27.153988);
        LatLng mPoint4 = new LatLng(38.433767, 27.155410);
        LatLng mPoint5 = new LatLng(38.432143, 27.156049);
        LatLng mPoint6 = new LatLng(38.427804, 27.153486);
        LatLng mPoint7 = new LatLng(38.424637, 27.152007);
        LatLng mPoint8 = new LatLng(38.421315, 27.153216);
        LatLng mPoint9 = new LatLng(38.418219, 27.154731);
        LatLng mPoint10 = new LatLng(38.414628, 27.156085);

        mPoints.add(mPoint1);
        mPoints.add(mPoint2);
        mPoints.add(mPoint3);
        mPoints.add(mPoint4);
        mPoints.add(mPoint5);
        mPoints.add(mPoint6);
        mPoints.add(mPoint7);
        mPoints.add(mPoint8);
        mPoints.add(mPoint9);
        mPoints.add(mPoint10);

        BitmapDescriptor bd = BitmapDescriptorFactory
                .defaultMarker(BitmapDescriptorFactory.HUE_VIOLET);

        for(int i = 0; i<mPoints.size(); i++){
            Marker marker = mMap.addMarker(
                    new MarkerOptions()
                            .visible(false)
                            .position(mPoints.get(i)).icon(bd));

            mMarkers.add(marker);
        }
    }

    private void filterMarkers(double radiusForCircle){
        mCircle.setRadius(radiusForCircle);
        float[] distance = new float[2];
        for(int m = 0; m < mMarkers.size(); m++){
            Marker marker = mMarkers.get(m);
            LatLng position = marker.getPosition();
            double lat = position.latitude;
            double lon = position.longitude;

            Location.distanceBetween(lat, lon, mCircleCenter.latitude,
                    mCircleCenter.longitude, distance);

            boolean inCircle = distance[0] <= radiusForCircle;
            marker.setVisible(inCircle);
        }
    }




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);

        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

        mRadiusSeekBar = (SeekBar) findViewById(R.id.seekbar);

        mRadiusSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                filterMarkers(progress * 10);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        mMap.moveCamera(CameraUpdateFactory
                .newLatLngZoom(mCircleCenter, 12));

        addMarkers();
        addCircle();
    }
}

布局

<FrameLayout android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.blackkara.mapquestions.MapsActivity" />

<android.support.v7.widget.AppCompatSeekBar
    android:id="@+id/seekbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"/>
</FrameLayout>