2016年10月16日 星期日

Google map api demo II : CircleDemoActivity 和 SplitStreetViewPanoramaAndMapDemoActivity



public class CircleDemoActivity extends AppCompatActivity implements OnSeekBarChangeListener,
        OnMarkerDragListener, OnMapLongClickListener, OnMapReadyCallback {}

先看   onCreate()


@Overrideprotected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.circle_demo);

    mColorBar = (SeekBar) findViewById(R.id.hueSeekBar);
    mColorBar.setMax(HUE_MAX);
    mColorBar.setProgress(0);

    mAlphaBar = (SeekBar) findViewById(R.id.alphaSeekBar);
    mAlphaBar.setMax(ALPHA_MAX);
    mAlphaBar.setProgress(127);

    mWidthBar = (SeekBar) findViewById(R.id.widthSeekBar);
    mWidthBar.setMax(WIDTH_MAX);
    mWidthBar.setProgress(10);

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

    mClickabilityCheckbox = (CheckBox) findViewById(R.id.toggleClickability);
}

有三個   colorBar...  和一個CheckBox....

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

    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center_horizontal"        android:text="@string/properties_circle" />

    <TableLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:stretchColumns="1">// 只有一欄

        <TableRow            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:gravity="center_vertical">

            <TextView android:text="@string/fill_hue" />

            <SeekBar android:id="@+id/hueSeekBar" />
        </TableRow>

        <TableRow            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:gravity="center_vertical">

            <TextView android:text="@string/fill_alpha" />

            <SeekBar android:id="@+id/alphaSeekBar" />
        </TableRow>

        <TableRow            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:gravity="center_vertical">

            <TextView android:text="@string/stroke_width" />

            <SeekBar android:id="@+id/widthSeekBar" />
        </TableRow>

        <TableRow            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:gravity="center_vertical">

            <CheckBox                android:id="@+id/toggleClickability"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:checked="true"                android:onClick="toggleClickability"                android:text="@string/clickable"/>
        </TableRow>

    </TableLayout>

    <fragment        android:id="@+id/map"        android:layout_width="match_parent"        android:layout_height="match_parent"        class="com.google.android.gms.maps.SupportMapFragment"/>

</LinearLayout>


先看  onMapReady()



@Overridepublic void onMapReady(GoogleMap map) {
    mMap = map;

    // Override the default content description on the view, for accessibility mode.    map.setContentDescription(getString(R.string.map_circle_description));

    mColorBar.setOnSeekBarChangeListener(this);
    mAlphaBar.setOnSeekBarChangeListener(this);
    mWidthBar.setOnSeekBarChangeListener(this);
    mMap.setOnMarkerDragListener(this);
    mMap.setOnMapLongClickListener(this);

    mFillColor = Color.HSVToColor(
            mAlphaBar.getProgress(), new float[]{mColorBar.getProgress(), 1, 1});
    mStrokeColor = Color.BLACK;

    DraggableCircle circle =
            new DraggableCircle(SYDNEY, DEFAULT_RADIUS, mClickabilityCheckbox.isChecked());
    mCircles.add(circle);

    // Move the map so that it is centered on the initial circle    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(SYDNEY, 4.0f));
    // Set up the click listener for the circle.    map.setOnCircleClickListener(new OnCircleClickListener() {
        @Override        public void onCircleClick(Circle circle) {
            // Flip the r, g and b components of the circle's stroke color.            int strokeColor = circle.getStrokeColor() ^ 0x00ffffff;
            circle.setStrokeColor(strokeColor);
        }
    });
}


private class DraggableCircle {
    private final Marker centerMarker;
    private final Marker radiusMarker;
    private final Circle circle;
    private double radius;
    public DraggableCircle(LatLng center, double radius, boolean clickable) {
        this.radius = radius;
        centerMarker = mMap.addMarker(new MarkerOptions()
                .position(center)
                .draggable(true));
        radiusMarker = mMap.addMarker(new MarkerOptions()
                .position(toRadiusLatLng(center, radius))
                .draggable(true)
                .icon(BitmapDescriptorFactory.defaultMarker(
                        BitmapDescriptorFactory.HUE_AZURE)));
        circle = mMap.addCircle(new CircleOptions()
                .center(center)
                .radius(radius)
                .strokeWidth(mWidthBar.getProgress())
                .strokeColor(mStrokeColor)
                .fillColor(mFillColor)
                .clickable(clickable));
    }

    public DraggableCircle(LatLng center, LatLng radiusLatLng, boolean clickable) {
        this.radius = toRadiusMeters(center, radiusLatLng);
        centerMarker = mMap.addMarker(new MarkerOptions()
                .position(center)
                .draggable(true));
        radiusMarker = mMap.addMarker(new MarkerOptions()
                .position(radiusLatLng)
                .draggable(true)
                .icon(BitmapDescriptorFactory.defaultMarker(
                        BitmapDescriptorFactory.HUE_AZURE)));
        circle = mMap.addCircle(new CircleOptions()
                .center(center)
                .radius(radius)
                .strokeWidth(mWidthBar.getProgress())
                .strokeColor(mStrokeColor)
                .fillColor(mFillColor)
                .clickable(clickable));
    }

    public boolean onMarkerMoved(Marker marker) {
        if (marker.equals(centerMarker)) {
            circle.setCenter(marker.getPosition());
            radiusMarker.setPosition(toRadiusLatLng(marker.getPosition(), radius));
            return true;
        }
        if (marker.equals(radiusMarker)) {
            radius = toRadiusMeters(centerMarker.getPosition(), radiusMarker.getPosition());
            circle.setRadius(radius);
            return true;
        }
        return false;
    }

    public void onStyleChange() {
        circle.setStrokeWidth(mWidthBar.getProgress());
        circle.setFillColor(mFillColor);
        circle.setStrokeColor(mStrokeColor);
    }

    public void setClickable(boolean clickable) {
        circle.setClickable(clickable);
    }
}




當常按時...新增一個circle.....

@Overridepublic void onMapLongClick(LatLng point) {
    // We know the center, let's place the outline at a point 3/4 along the view.    View view = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
            .getView();
    LatLng radiusLatLng = mMap.getProjection().fromScreenLocation(new Point(
            view.getHeight() * 3 / 4, view.getWidth() * 3 / 4));

    // ok create it    DraggableCircle circle =
            new DraggableCircle(point, radiusLatLng, mClickabilityCheckbox.isChecked());
    mCircles.add(circle);   //  新增一個  circles
}


//  當按下   checkbox  -> toggleClickability

public void toggleClickability(View view) {
    boolean clickable = ((CheckBox) view).isChecked();
    // Set each of the circles to be clickable or not, based on the    // state of the checkbox.    for (DraggableCircle draggableCircle : mCircles) {
        draggableCircle.setClickable(clickable);
    }
}



@Overridepublic void onMarkerDragStart(Marker marker) {
    onMarkerMoved(marker);
}

@Overridepublic void onMarkerDragEnd(Marker marker) {
    onMarkerMoved(marker);
}

@Overridepublic void onMarkerDrag(Marker marker) {
    onMarkerMoved(marker);
}

private void onMarkerMoved(Marker marker) {
    for (DraggableCircle draggableCircle : mCircles) {
        if (draggableCircle.onMarkerMoved(marker)) {
            break;
        }
    }
}


=======================================================================================================

public class SplitStreetViewPanoramaAndMapDemoActivity extends AppCompatActivity
        implements OnMarkerDragListener, OnStreetViewPanoramaChangeListener {}

@Overrideprotected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.split_street_view_panorama_and_map_demo);

    final LatLng markerPosition;
    if (savedInstanceState == null) {
        markerPosition = SYDNEY;
    } else {
        markerPosition = savedInstanceState.getParcelable(MARKER_POSITION_KEY);
    }

    SupportStreetViewPanoramaFragment streetViewPanoramaFragment =
            (SupportStreetViewPanoramaFragment)
                    getSupportFragmentManager().findFragmentById(R.id.streetviewpanorama);
    streetViewPanoramaFragment.getStreetViewPanoramaAsync(
            new OnStreetViewPanoramaReadyCallback() {
                @Override                public void onStreetViewPanoramaReady(StreetViewPanorama panorama) {
                    mStreetViewPanorama = panorama;
                    mStreetViewPanorama.setOnStreetViewPanoramaChangeListener(
                            SplitStreetViewPanoramaAndMapDemoActivity.this);
                    // Only need to set the position once as the streetview fragment will maintain                    // its state.                    if (savedInstanceState == null) {
                        mStreetViewPanorama.setPosition(SYDNEY);
                    }
                }
            });

    SupportMapFragment mapFragment =
            (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
    mapFragment.getMapAsync(new OnMapReadyCallback() {
        @Override        public void onMapReady(GoogleMap map) {
            map.setOnMarkerDragListener(SplitStreetViewPanoramaAndMapDemoActivity.this);
            // Creates a draggable marker. Long press to drag.            mMarker = map.addMarker(new MarkerOptions()
                    .position(markerPosition)
                    .icon(BitmapDescriptorFactory.fromResource(R.drawable.pegman))
                    .draggable(true));
        }
    });
}















沒有留言:

張貼留言