获取所需区域的经纬度坐标并存储在数据库中,从数据库中获取坐标,然后在地图上绘制多边形。
确保检查您的 Android 清单
请注意应用的 AndroidManifest.xml 文件中的以下元素:
添加元数据元素以嵌入编译应用时使用的 Google Play 服务版本。
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
添加指定 API 密钥的元数据元素。本教程附带的示例将 API 密钥的值映射到字符串 google_maps_key。当您构建应用程序时,Gradle 会将 API 密钥从项目的 Gradle.properties 文件复制到字符串值。
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.polygons">
以下是清单的完整示例:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<!--
The API key for Google Maps-based APIs.
-->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
<activity
android:name="com.example.polygons.PolyActivity"
android:label="@string/title_activity_maps">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
添加地图
使用 Maps SDK for Android 显示地图。
将<fragment> 元素添加到您的活动布局文件activity_maps.xml。此元素定义了一个 SupportMapFragment 以充当地图的容器并提供对 GoogleMap 对象的访问。本教程使用地图片段的 Android 支持库版本,以确保向后兼容早期版本的 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.example.polygons.PolyActivity" />
在活动的 onCreate() 方法中,将布局文件设置为内容视图。通过调用 FragmentManager.findFragmentById() 获取地图片段的句柄。然后使用 getMapAsync() 注册地图回调:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Retrieve the content view that renders the map.
setContentView(R.layout.activity_maps);
// Get the SupportMapFragment and request notification when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(map);
mapFragment.getMapAsync(this);
}
实现 OnMapReadyCallback 接口并重写 onMapReady() 方法。当 GoogleMap 对象可用时,API 会调用此回调,因此您可以将对象添加到地图并为您的应用进一步自定义:
public class PolyActivity extends AppCompatActivity
implements
OnMapReadyCallback,
GoogleMap.OnPolylineClickListener,
GoogleMap.OnPolygonClickListener {
// More code goes here, including the onCreate() method described above.
@Override
public void onMapReady(GoogleMap googleMap) {
// Add polylines and polygons to the map. This section shows just
// a single polyline. Read the rest of the tutorial to learn more.
Polyline polyline1 = googleMap.addPolyline(new PolylineOptions()
.clickable(true)
.add(
new LatLng(-35.016, 143.321),
new LatLng(-34.747, 145.592),
new LatLng(-34.364, 147.891),
new LatLng(-33.501, 150.217),
new LatLng(-32.306, 149.248),
new LatLng(-32.491, 147.309)));
// Position the map's camera near Alice Springs in the center of Australia,
// and set the zoom factor so most of Australia shows on the screen.
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(-23.684, 133.903), 4));
// Set listeners for click events.
googleMap.setOnPolylineClickListener(this);
googleMap.setOnPolygonClickListener(this);
}
}