Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google maps Google将API捕捉标记映射到多段线_Google Maps_Google Maps Api 3 - Fatal编程技术网

Google maps Google将API捕捉标记映射到多段线

Google maps Google将API捕捉标记映射到多段线,google-maps,google-maps-api-3,Google Maps,Google Maps Api 3,我有一个在谷歌地图上绘制的多段线列表(来自谷歌方向API)。接下来,我想让我的标记沿着这条线移动,而不使用谷歌地图道路API 为了解决这个问题,我目前正在做以下工作: 从“多段线”列表中,我选择路径A和B的两个端点 从A到B找出轴承 创建a和B之间相距1米的LatLng点列表 迭代列表以检查最近的LatLng的GPS坐标 将我的标记位置设置为最近的LatLng 上述操作在某些情况下有效,但大多数情况下,标记与多段线平行显示,而不是在其上 是否有其他方法使我的标记捕捉到多段线?以下是我自己将标记捕

我有一个在谷歌地图上绘制的多段线列表(来自谷歌方向API)。接下来,我想让我的标记沿着这条线移动,而不使用谷歌地图道路API

为了解决这个问题,我目前正在做以下工作:

  • 从“多段线”列表中,我选择路径A和B的两个端点
  • 从A到B找出轴承
  • 创建a和B之间相距1米的
    LatLng
    点列表
  • 迭代列表以检查最近的
    LatLng
    的GPS坐标
  • 将我的标记位置设置为最近的
    LatLng
  • 上述操作在某些情况下有效,但大多数情况下,标记与多段线平行显示,而不是在其上


    是否有其他方法使我的标记捕捉到多段线?

    以下是我自己将标记捕捉到多段线的实现(基于GPS位置):

    首先,使用多段线点创建一个距离较小的
    LatLng
    列表(我使用的是
    splitPathIntoPoints
    函数中指定的一米距离):

    查找状态的代码:

    public static Float findDistance(LatLng source, LatLng destination) {
        Location srcLoc = new Location("srcLoc");
        srcLoc.setLatitude(source.latitude);
        srcLoc.setLongitude(source.longitude);
    
        Location destLoc = new Location("destLoc");
        destLoc.setLatitude(destination.latitude);
        destLoc.setLongitude(destination.longitude);
    
        return srcLoc.distanceTo(destLoc);
    }
    
    findMidPoint的代码

    public static LatLng findMidPoint(LatLng source, LatLng destination) {
        double x1 = toRad(source.latitude);
        double y1 = toRad(source.longitude);
    
        double x2 = toRad(destination.latitude);
        double y2 = toRad(destination.longitude);
    
        double Bx = Math.cos(x2) * Math.cos(y2 - y1);
        double By = Math.cos(x2) * Math.sin(y2 - y1);
        double x3 = toDeg(Math.atan2(Math.sin(x1) + Math.sin(x2), Math.sqrt((Math.cos(x1) + Bx) * (Math.cos(x1) + Bx) + By * By)));
        double y3 = y1 + Math.atan2(By, Math.cos(x1) + Bx);
        y3 = toDeg((y3 + 540) % 360 - 180);
    
        return new LatLng(x3, y3);
    }
    
    一旦
    mSplitPoints
    填充了彼此间隔1米的较小多段线点,下面的函数将根据我当前的GPS位置查找多段线上的捕捉位置。请注意,
    mminoindex traveled
    是我的类中的一个私有字段,初始值设置为零

    public static LatLng snapToPolyline(LatLng currentLocation) {
        LatLng snappedLatLng = null;
    
        Location current = new Location("current");
        current.setLatitude(currentLocation.latitude);
        current.setLongitude(currentLocation.longitude);
    
        Integer minConfirmCount = 0;
        float currentMinDistance = 0, previousMinDistance = 0;
        List<Float> distances = new ArrayList<>();
    
        for (LatLng point: mSplitPoints.subList(mMinorIndexTravelled, mSplitPoints.size() - 1)) {
            Location pointLoc = new Location("pointLoc");
            pointLoc.setLatitude(point.latitude);
            pointLoc.setLongitude(point.longitude);
    
            distances.add(current.distanceTo(pointLoc));
            previousMinDistance = currentMinDistance;
            currentMinDistance = Collections.min(distances);
    
            if (currentMinDistance == previousMinDistance) {
                minConfirmCount++;
                if (minConfirmCount > 10) {
                    mMinorIndexTravelled = distances.indexOf(currentMinDistance) + mMinorIndexTravelled;
                    snappedLatLng = mSplitPoints.get(mMinorIndexTravelled);
    
                    break;
                }
            }
        }
    
        return snappedLatLng;
    }
    
    public static LatLng snapToPolyline(LatLng currentLocation){
    LatLng SNAPDLATLNG=null;
    当前位置=新位置(“当前”);
    当前.设置纬度(当前位置.纬度);
    当前.设置经度(当前位置.经度);
    整数minConfirmCount=0;
    float currentMindDistance=0,previousMindDistance=0;
    列表距离=新的ArrayList();
    对于(LatLng point:mSplitPoints.subList(mminoIndexTravelled,mSplitPoints.size()-1)){
    位置点定位=新位置(“点定位”);
    点位置设置纬度(点纬度);
    点位置设置经度(点经度);
    距离。添加(当前距离到(点位置));
    previousMinDistance=当前MinDistance;
    currentMinDistance=Collections.min(距离);
    if(currentMindDistance==previousMindDistance){
    minConfirmCount++;
    如果(minConfirmCount>10){
    mminorIndexTraveled=距离。indexOf(CurrentMindDistance)+mminorIndexTraveled;
    snapdlatlng=mSplitPoints.get(mminoredextraveled);
    打破
    }
    }
    }
    返回快照LATLNG;
    }
    
    相关问题:谢谢@geocodezip,但本例将标记捕捉到多段线的端点。不适用于两者之间的任何像素/坐标。如果您运行答案中提供的POC,请尝试在A.N.C.道路上捕捉标记。
    public static LatLng findMidPoint(LatLng source, LatLng destination) {
        double x1 = toRad(source.latitude);
        double y1 = toRad(source.longitude);
    
        double x2 = toRad(destination.latitude);
        double y2 = toRad(destination.longitude);
    
        double Bx = Math.cos(x2) * Math.cos(y2 - y1);
        double By = Math.cos(x2) * Math.sin(y2 - y1);
        double x3 = toDeg(Math.atan2(Math.sin(x1) + Math.sin(x2), Math.sqrt((Math.cos(x1) + Bx) * (Math.cos(x1) + Bx) + By * By)));
        double y3 = y1 + Math.atan2(By, Math.cos(x1) + Bx);
        y3 = toDeg((y3 + 540) % 360 - 180);
    
        return new LatLng(x3, y3);
    }
    
    public static LatLng snapToPolyline(LatLng currentLocation) {
        LatLng snappedLatLng = null;
    
        Location current = new Location("current");
        current.setLatitude(currentLocation.latitude);
        current.setLongitude(currentLocation.longitude);
    
        Integer minConfirmCount = 0;
        float currentMinDistance = 0, previousMinDistance = 0;
        List<Float> distances = new ArrayList<>();
    
        for (LatLng point: mSplitPoints.subList(mMinorIndexTravelled, mSplitPoints.size() - 1)) {
            Location pointLoc = new Location("pointLoc");
            pointLoc.setLatitude(point.latitude);
            pointLoc.setLongitude(point.longitude);
    
            distances.add(current.distanceTo(pointLoc));
            previousMinDistance = currentMinDistance;
            currentMinDistance = Collections.min(distances);
    
            if (currentMinDistance == previousMinDistance) {
                minConfirmCount++;
                if (minConfirmCount > 10) {
                    mMinorIndexTravelled = distances.indexOf(currentMinDistance) + mMinorIndexTravelled;
                    snappedLatLng = mSplitPoints.get(mMinorIndexTravelled);
    
                    break;
                }
            }
        }
    
        return snappedLatLng;
    }