0

我搜索并找不到答案,所以我假设我是这里唯一一个迷路的人。

一个在 Android 地图上工作的新手,想知道是否有一个很好的资源可以用来设置一个 TileServer,然后可以在 Android 地图中使用。

我的目标是让谷歌地图以多边形的形式覆盖地理空间信息。

4

1 回答 1

1

您可以尝试以下代码,该代码根据纬度值在谷歌地图上绘制多边形,并管理其色调、Aplha、笔画宽度。

PolygonDemo 类

 public class PolygonDemoActivity extends android.support.v4.app.FragmentActivity
    implements OnSeekBarChangeListener {
private static final LatLng SYDNEY = new LatLng(-33.87365, 151.20689);
private static final int WIDTH_MAX = 50;
private static final int HUE_MAX = 360;
private static final int ALPHA_MAX = 255;
private GoogleMap mMap;
private Polygon mMutablePolygon;
private SeekBar mColorBar;
private SeekBar mAlphaBar;
private SeekBar mWidthBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.polygon_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);
    setUpMapIfNeeded();
}
@Override
protected void onResume() {
    super.onResume();
    setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
    // Do a null check to confirm that we have not already instantiated the map.
    if (mMap == null) {
        // Try to obtain the map from the SupportMapFragment.
        mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                .getMap();
        // Check if we were successful in obtaining the map.
        if (mMap != null) {
            setUpMap();
        }
    }
}
private void setUpMap() {
    // Create a rectangle with two rectangular holes.
    mMap.addPolygon(new PolygonOptions()
            .addAll(createRectangle(new LatLng(-20, 130), 5, 5))
            .addHole(createRectangle(new LatLng(-22, 128), 1, 1))
            .addHole(createRectangle(new LatLng(-18, 133), 0.5, 1.5))
            .fillColor(Color.CYAN)
            .strokeColor(Color.BLUE)
            .strokeWidth(5));
    // Create an ellipse centered at Sydney.
    PolygonOptions options = new PolygonOptions();
    int numPoints = 400;
    float semiHorizontalAxis = 10f;
    float semiVerticalAxis = 5f;
    double phase = 2 * Math.PI / numPoints;
    for (int i = 0; i <= numPoints; i++) {
        options.add(new LatLng(SYDNEY.latitude + semiVerticalAxis * Math.sin(i * phase),
                SYDNEY.longitude + semiHorizontalAxis * Math.cos(i * phase)));
    }
    int fillColor = Color.HSVToColor(
            mAlphaBar.getProgress(), new float[] {mColorBar.getProgress(), 1, 1});
    mMutablePolygon = mMap.addPolygon(options
            .strokeWidth(mWidthBar.getProgress())
            .strokeColor(Color.BLACK)
            .fillColor(fillColor));
    mColorBar.setOnSeekBarChangeListener(this);
    mAlphaBar.setOnSeekBarChangeListener(this);
    mWidthBar.setOnSeekBarChangeListener(this);
    // Move the map so that it is centered on the mutable polygon.
    mMap.moveCamera(CameraUpdateFactory.newLatLng(SYDNEY));
}
/**
 * Creates a List of LatLngs that form a rectangle with the given dimensions.
 */
private List<LatLng> createRectangle(LatLng center, double halfWidth, double halfHeight)              
   {        // Note that the ordering of the points is counterclockwise (as long as the halfWidth and
    // halfHeight are less than 90).
    return Arrays.asList(new LatLng(center.latitude - halfHeight, center.longitude - halfWidth),
            new LatLng(center.latitude - halfHeight, center.longitude + halfWidth),
            new LatLng(center.latitude + halfHeight, center.longitude + halfWidth),
            new LatLng(center.latitude + halfHeight, center.longitude - halfWidth),
            new LatLng(center.latitude - halfHeight, center.longitude - halfWidth));
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
    // Don't do anything here.
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
    // Don't do anything here.
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    if (mMutablePolygon == null) {
        return;
    }
    if (seekBar == mColorBar) {
        mMutablePolygon.setFillColor(Color.HSVToColor(
                Color.alpha(mMutablePolygon.getFillColor()), new float[] {progress, 1, 1}));
    } else if (seekBar == mAlphaBar) {
        int prevColor = mMutablePolygon.getFillColor();
        mMutablePolygon.setFillColor(Color.argb(
                progress, Color.red(prevColor), Color.green(prevColor),
                Color.blue(prevColor)));
    } else if (seekBar == mWidthBar) {
        mMutablePolygon.setStrokeWidth(progress);
    }
}
}

多边形演示.xml

 <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_sydney_polygon"/>
    <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>
    </TableLayout>
    <fragment
            android:id="@+id/map"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            class="com.google.android.gms.maps.SupportMapFragment"/>
  </LinearLayout>

有关更多详细信息,您可以查看链接,该链接显示了在地图上绘制多边形的演示实现。

我希望它会帮助你。

谢谢。

于 2013-02-06T05:53:52.173 回答