如何在折线路径上设置google地图v2标记的动画?

 手机用户敬怡 发布于 2023-02-10 14:38

我已经使用谷歌地图V2构建了一个应用程序,并在地图上放置了2个标记.第一个标记是获取用户当前位置,第二个标记是用户目标位置.然后我添加decodePoly方法来绘制这些标记之间的界限.我还添加了一个方法,使用警告对话框为用户提供有关持续时间,起始地址和目标地址的信息.

路线

因此,当我单击第二个标记时,我想要将该标记设置为"移动"到第一个标记.但我的问题是,第二个标记没有在折线路径上移动.你可以在下面的图片看到:

结果

这是MapActivity.java:

@Override
public boolean onMarkerClick(Marker arg0) { // if user click the first marker 

    if(this.lokasi_asli.equals(arg0)){
        AlertDialog.Builder alert = new AlertDialog.Builder(MapActivity.this);
        alert.setTitle("First Position")
            .setIcon(R.drawable.ic_launcher)
            .setCancelable(true)
            .setMessage("First Position : " + "\n" + loc_a)
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                    dialog.dismiss();
                }
            })
            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                    dialog.dismiss();
                }
            });
        alert.show();
    } else if (this.lokasi_tujuan.equals(arg0)) { //if user click second marker
        final boolean hideMarker = false;
        final Handler handler = new Handler();
        final long start = SystemClock.uptimeMillis();
        Projection proj = gmap.getProjection();
        Point startPoint = proj.toScreenLocation(lokasi_tujuan.getPosition());
        final LatLng startLatLng = proj.fromScreenLocation(startPoint);
        final long duration = 5000;
        final Interpolator interpolator = new LinearInterpolator();
        handler.post(new Runnable() {

            @Override
            public void run() {
                long elapsed = SystemClock.uptimeMillis() - start;
                float t = interpolator.getInterpolation((float) elapsed / duration);
                double longid = t * lokasi_asli.getPosition().longitude + (1-t) * startLatLng.longitude;
                double latid = t * lokasi_asli.getPosition().latitude + (1-t) * startLatLng.latitude;
                lokasi_tujuan.setPosition(new LatLng(latid, longid));
                if(t < 1.0){
                    //
                    handler.postDelayed(this, 16);
                } else {
                    if(hideMarker){
                        lokasi_tujuan.setVisible(false);
                    } else {
                        lokasi_tujuan.setVisible(true);
                    }
                }
            }

        });

从谷歌地图获取数据:

@Override
    protected void onPostExecute(Void result){
        super.onPostExecute(result);
        if(doc != null){
            NodeList _nodelist = doc.getElementsByTagName("status");
            Node node1 = _nodelist.item(0);
            String _status1 = node1.getChildNodes().item(0).getNodeValue();
            if(_status1.equalsIgnoreCase("OK"))
            {
                NodeList _nodelist_path = doc.getElementsByTagName("overview_polyline");
                Node node_path = _nodelist_path.item(0);
                Element _status_path = (Element)node_path;
                NodeList _nodelist_destination_path = _status_path.getElementsByTagName("points");
                Node _nodelist_dest = _nodelist_destination_path.item(0);
                String _path = _nodelist_dest.getChildNodes().item(0).getNodeValue();
                List directionPoint = decodePoly(_path);

                PolylineOptions rectLine = new PolylineOptions().width(10).color(Color.RED).geodesic(true);
                for (int i = 0; i < directionPoint.size(); i++) 
                {
                    rectLine.add(directionPoint.get(i));
                }

                gmap.addPolyline(rectLine);
                mark_opt.position(new LatLng(dest_lat, dest_long));
                mark_opt.draggable(true);
                mark_opt.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE));
                lokasi_tujuan = gmap.addMarker(mark_opt);
            } else {
                Toast.makeText(MapActivity.this, "Maaf, Tidak dapat menemukan rute tujuan\nCoba pilih rute lain yang mendekati", Toast.LENGTH_LONG).show();
            }
            progres_dlg.dismiss();
            }
        }
    }

如何让第二个标记在折线路径上移动?任何想法或帮助将不胜感激.环境:Windows 7,Eclipse,Genymotion.

1 个回答
  • 查看教程,其中显示了如何在地图路线上设置标记动画.它还提供了GitHub中的代码,您可以尝试使用它.

    点击这里

    希望这会指导你.

    2023-02-10 14:40 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有