Android Studio: 更新/删除折线或点,无需再次调用Google Directions-API
创始人
2024-08-17 01:30:13
0

要实现在Android Studio中更新/删除折线或点,无需再次调用Google Directions-API,可以使用Polyline和Marker对象来管理折线和点的更新和删除。

首先,在您的项目中添加Google Maps Android API库的依赖项。在您的build.gradle文件中添加以下代码:

dependencies {
    implementation 'com.google.android.gms:play-services-maps:17.0.1'
}

然后,在您的布局文件中添加一个MapView元素:


接下来,在您的Activity或Fragment中,初始化MapView和GoogleMap对象,并添加Polyline和Marker对象:

private MapView mapView;
private GoogleMap googleMap;
private Polyline polyline;
private Marker marker;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mapView = findViewById(R.id.mapView);
    mapView.onCreate(savedInstanceState);
    mapView.getMapAsync(new OnMapReadyCallback() {
        @Override
        public void onMapReady(GoogleMap map) {
            googleMap = map;
            // 初始化地图完成后的操作
            // 添加默认的折线和点
            addDefaultPolyline();
            addDefaultMarker();
        }
    });
}

private void addDefaultPolyline() {
    // 创建折线的坐标点列表
    List points = new ArrayList<>();
    points.add(new LatLng(37.7749, -122.4194));
    points.add(new LatLng(34.0522, -118.2437));
    points.add(new LatLng(32.7157, -117.1611));

    // 创建折线对象
    PolylineOptions polylineOptions = new PolylineOptions()
            .addAll(points)
            .color(Color.RED)
            .width(5);
    polyline = googleMap.addPolyline(polylineOptions);
}

private void addDefaultMarker() {
    // 创建标记点的位置
    LatLng position = new LatLng(37.7749, -122.4194);

    // 创建标记点对象
    MarkerOptions markerOptions = new MarkerOptions()
            .position(position)
            .title("San Francisco");

    marker = googleMap.addMarker(markerOptions);
}

private void updatePolyline() {
    // 更新折线的坐标点列表
    List newPoints = new ArrayList<>();
    newPoints.add(new LatLng(37.7749, -122.4194));
    newPoints.add(new LatLng(47.6062, -122.3321));
    newPoints.add(new LatLng(34.0522, -118.2437));

    // 更新折线对象的坐标点
    polyline.setPoints(newPoints);
}

private void updateMarker() {
    // 更新标记点的位置
    LatLng newPosition = new LatLng(47.6062, -122.3321);

    // 更新标记点对象的位置
    marker.setPosition(newPosition);
}

private void removePolyline() {
    // 移除折线对象
    polyline.remove();
}

private void removeMarker() {
    // 移除标记点对象
    marker.remove();
}

@Override
protected void onResume() {
    super.onResume();
    mapView.onResume();
}

@Override
protected void onPause() {
    super.onPause();
    mapView.onPause();
}

@Override
protected void onDestroy() {
    super.onDestroy();
    mapView.onDestroy();
}

@Override
public void onLowMemory() {
    super.onLowMemory();
    mapView.onLowMemory();
}

在上面的示例代码中,我们首先在onMapReady回调方法中初始化了MapView和GoogleMap对象,并添加了默认的折线和点。然后,我们定义了updatePolyline和updateMarker方法来更新折线和点的位置。最后,我们还定义了removePolyline和removeMarker方法来删除折线和点。

您可以根据自己的需求调用这些方法来更新或删除折线和点,而无需再次调用Google Directions-API。记得在更新或删除折线和点时,确保在地图准备就绪后进行操作。

相关内容

热门资讯

此事备受玩家关注!wepoke... 此事备受玩家关注!wepoker透视辅助下载,wepoker轻量版辅助,诀窍教程(有挂规律)-哔哩哔...
出乎意料的是!wepoker私... 出乎意料的是!wepoker私局代打(透视)原来有挂(有挂秘诀开挂辅助安装)-哔哩哔哩1、点击下载安...
围绕透视问题!aapoker辅... 围绕透视问题!aapoker辅助工具安全吗,淘宝买wepoker透视有用吗,操作教程(揭秘有挂)-哔...
为了进一步!pokemmo手机... 为了进一步!pokemmo手机版透视脚本(透视)一直真的是有挂(有挂秘笈开挂辅助工具)-哔哩哔哩1、...
现就发布提示!hhpoker破... 现就发布提示!hhpoker破解工具,wepokerh5破解,手册教程(有挂分享)-哔哩哔哩1、用户...
为切实保障!德普之星的辅助工具... 为切实保障!德普之星的辅助工具介绍(透视)其实确实有挂(讲解有挂开挂辅助安装)-哔哩哔哩1、每一步都...
今天上午!wejoker手机版... 今天上午!wejoker手机版透视脚本,newpoker怎么安装脚本,讲义教程(真是有挂)-哔哩哔哩...
据统计!hhpoker可以开挂... 据统计!hhpoker可以开挂吗(透视)一直真的有挂(有挂详细开挂辅助辅助器)-哔哩哔哩1、上手简单...
刚刚!约局吧德州真的存在透视吗... 刚刚!约局吧德州真的存在透视吗,pokemmo辅助工具,教材教程(今日头条)-哔哩哔哩1、约局吧德州...
据报道!哈糖大菠萝免费辅助器(... 据报道!哈糖大菠萝免费辅助器(透视)原来真的是有挂(有挂教学开挂辅助插件)-哔哩哔哩1、哈糖大菠萝免...