【问题标题】:Is there a way to highlight an area within a city using the google map api?有没有办法使用谷歌地图 api 突出显示城市内的一个区域?
【发布时间】:2019-07-29 01:58:46
【问题描述】:

我已经在我的应用程序上设置了谷歌地图,我想向用户展示他们在这样的城市中所处的区域,

然而,这是一个桌面浏览器,我想在我的应用中实现。

这样我可以得到一个地区名称而不是整个城市,因为它太宽泛了,或者一个地址太具体了。

这是我获取用户位置后的onMapReady

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

    // Add a marker in Sydney and move the camera
    LatLng currentLocation = new LatLng(currentLatitude, currentLogitude);
    mMap.addMarker(new MarkerOptions().position(currentLocation).title("Current Location"));
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLocation, 14.0f));
}

【问题讨论】:

  • 您可以为 google map api 制作多边形,但您需要使用 LatLngBounds 了解您所在城市或任何区域的边界
  • @PayamKokabi 就像我说的,我不想只勾勒出城市中的一个区域,这也会很痛苦,因为太多了
  • @Kristofer 你仍然需要知道边界提供坐标,地图不会神奇地为你做。 developers.google.com/maps/documentation/android-sdk/shapes
  • @Leo 那么谷歌地图是如何做到这一点的呢?我的意思是,他们必须手动手动完成数千个区域。
  • @Kristofer Google 就是这样做的。你真的认为几千个区域很多吗?再想想……

标签: android google-maps


【解决方案1】:

获取所需区域的经纬度坐标并存储在数据库中,从数据库中获取坐标,然后在地图上绘制多边形。

确保检查您的 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 显示地图。

&lt;fragment&gt; 元素添加到您的活动布局文件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);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-29
    • 2015-02-21
    • 1970-01-01
    • 1970-01-01
    • 2013-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多