我们如何拖动矩形,我们点击地图

时间:2017-11-13 10:03:41

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

我们如何获得矩形坐标来绘制矩形,我们点击谷歌地图?我想绘制矩形我只获得谷歌地图点击功能后的纬度和经度,请帮助`公共类MapsActivity extends FragmentActivity实现了OnMapReadyCallback {

private static final LatLng LOWER_MANHATTAN = new LatLng(22.718435,
        75.855217);
private static final LatLng BROOKLYN_BRIDGE = new LatLng(22.7244, 75.8839);
static Double latitudeS = 22.719568;
static Double longitudeE = 75.857727;
static Double LatitudeS = 22.7533;
static Double LongitudeE = 75.8840;
Circle circle;
boolean isClicable = true;
Button clearbtn;
int radiusofcircle = 1000;


Button increasepolygon;
private static final LatLng WALL_STREET = new LatLng(22.7533, 75.8937);
Handler m_handler;
Runnable m_handlerTask;
int t = 0;
ArrayList<LatLng> arrayList;
Button Zoomin, Zoomout;
ZoomControls simpleZoomControls;


GoogleMap mMap;
final String TAG = "PathGoogleMapActivity";
int zoomLevel;


@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);
    increasepolygon = (Button) findViewById(R.id.Increasepolygon);
    arrayList = new ArrayList<>();
    arrayList.add(LOWER_MANHATTAN);
    Zoomin = (Button) findViewById(R.id.zoomin);
    Zoomout = (Button) findViewById(R.id.Zoomout);
    clearbtn = (Button) findViewById(R.id.clearBtn);


}

private String getMapsApiDirectionsUrl() {
    String waypoints = "waypoints=optimize:true|"
            + LOWER_MANHATTAN.latitude + "," + LOWER_MANHATTAN.longitude
            + "|" + "|" + BROOKLYN_BRIDGE.latitude + ","
            + BROOKLYN_BRIDGE.longitude + "|" + WALL_STREET.latitude + ","
            + WALL_STREET.longitude;

    String sensor = "sensor=false";
    String params = waypoints + "&" + sensor;
    String output = "json";
    String url = "https://maps.googleapis.com/maps/api/directions/"
            + output + "?" + params;
    return url;
}

private void addMarkers() {
    if (mMap != null) {
        mMap.addMarker(new MarkerOptions().position(BROOKLYN_BRIDGE)
                .title("First Point"));
        mMap.addMarker(new MarkerOptions().position(LOWER_MANHATTAN)
                .title("Second Point"));
        mMap.addMarker(new MarkerOptions().position(WALL_STREET)
                .title("Third Point"));
    }
}


@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    MarkerOptions options = new MarkerOptions();
    options.position(LOWER_MANHATTAN);
    options.position(BROOKLYN_BRIDGE);
    options.position(WALL_STREET);
    options.flat(true);
    mMap.addMarker(options);


    // Polylines are useful for marking paths and routes on the map.


    PolylineOptions rectOptions = new PolylineOptions()
            .add(new LatLng(LatitudeS, LongitudeE))
            .add(new LatLng(latitudeS, LongitudeE))  // North of the previous point, but at the same longitude
            .add(new LatLng(latitudeS, longitudeE))  // Same latitude, and 30km to the west
            .add(new LatLng(LatitudeS, longitudeE))
            .add(new LatLng(LatitudeS, LongitudeE)); // Same longitude, and 16km to the south

    Polyline polyline = mMap.addPolyline(rectOptions);


    increasepolygon.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            LatitudeS += 0.00012;
            Polyline polyline = mMap.addPolyline(new PolylineOptions()

                    .add(new LatLng(latitudeS, longitudeE)
                            , new LatLng(LatitudeS, LongitudeE))
                    .width(5));
            mMap.addPolygon(new PolygonOptions().add(new LatLng(latitudeS, LongitudeE)));


        }
    });


    mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {

        @Override
        public void onMapClick(LatLng point) {
            if (isClicable == true) {
                drawCircleonclick(point);
                latitudeS = point.latitude;
                longitudeE = point.longitude;


                isClicable = false;
            }


        }
    });

// ReadTask downloadTask = new ReadTask(); // downloadTask.execute(url);

    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(BROOKLYN_BRIDGE,
            13));
    addMarkers();
}


private void drawRect() {

}


private void drawBounds(LatLngBounds bounds, LatLng point) {
    Log.d(TAG, "drawBounds" + bounds.northeast.latitude + " " + bounds.northeast.longitude + " " + bounds.southwest.latitude + " " + bounds.southwest.longitude);
    PolygonOptions polygonOptions = new PolygonOptions()

            .add(new LatLng(bounds.northeast.latitude, bounds.northeast.longitude))
            .add(new LatLng(bounds.southwest.latitude, bounds.northeast.longitude))
            .add(new LatLng(bounds.southwest.latitude, bounds.southwest.longitude))
            .add(new LatLng(bounds.northeast.latitude, bounds.southwest.longitude));

    mMap.addPolygon(polygonOptions);
}


public static final LatLngBounds BOUNDS_INDIA = new LatLngBounds(new LatLng(latitudeS, longitudeE), new LatLng(LatitudeS, LongitudeE));


public void drawCircleonclick(final LatLng point) {
    drawBounds(BOUNDS_INDIA, point);

    drawCircleOnClick(point);
    Zoomout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            radiusofcircle += 100;
            drawCircleonclick(point);
            drawBounds(BOUNDS_INDIA, point);
        }
    });

    Zoomin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            radiusofcircle -= 100;
            drawCircleonclick(point);
            drawBounds(BOUNDS_INDIA, point);
        }
    });

    clearbtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            latitudeS = 0.0d;
            longitudeE = 0.0d;
            mMap.clear();


            isClicable = true;

        }
    });


}


LatLng point;

private void drawCircleOnClick(LatLng point) {
    this.point = point;
    latitudeS = point.latitude;
    longitudeE = point.longitude;
    circle = mMap.addCircle(new CircleOptions()
            .center(new LatLng(latitudeS, longitudeE))
            .radius(radiusofcircle)

            .strokeColor(Color.RED)
            .fillColor(Color.BLUE));

}


public int getZoomLevel(Circle circle) {
    if (circle != null) {
        double radius = circle.getRadius();
        double scale = radius / 500;
        zoomLevel = (int) (16 - Math.log(scale) / Math.log(2));
    }
    return zoomLevel;
}

} `

0 个答案:

没有答案