首页 \ 问答 \ 如何让谷歌地图V3多段线从给定点捕捉道路?(How to make Google Maps V3 polyline snap to road from given points?)

如何让谷歌地图V3多段线从给定点捕捉道路?(How to make Google Maps V3 polyline snap to road from given points?)

我尝试通过给定的标记点将多段线捕捉到道路上。 我的问题是,相同的代码有时会给出好的结果,就像这张图片一样 在这里输入图像描述

有时候结果很糟糕,像这样: 在这里输入图像描述

任何想法,为什么这是头痛? 而且,折线对道路有没有限制?

我的地图ini代码:

var myLatlng = new google.maps.LatLng(47.6557, 23.5833);
var mapOptions = {
    zoom: 14,
    minZoom: 13,
    maxZoom: 19,
    center: myLatlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    disableDefaultUI:   true,
    overviewMapControl: false,
    streetViewControl:  false,
    scaleControl:       false,
    mapTypeControl:     false,
    panControl:         true,
    panControlOptions:{
        position: google.maps.ControlPosition.TOP_RIGHT
    },
    zoomControl: true,
    zoomControlOptions: {
        style: google.maps.ZoomControlStyle.LARGE,
        position: google.maps.ControlPosition.TOP_RIGHT
    }
}
var map = new google.maps.Map(document.getElementById("map"), mapOptions);

我的折线路线管理代码:

var polys = new google.maps.Polyline({
                map: map,
                strokeColor: "#5555FF"
            });
    myCoord = [
                        new google.maps.LatLng(47.663383463156144, 23.58100461977301),
                        new google.maps.LatLng(47.659221287827435, 23.586240291770082),
                        new google.maps.LatLng(47.65534785438211, 23.576713085349184),
                        new google.maps.LatLng(47.66020405359421, 23.572249889548402)
            ]; 

    // BEGIN: Snap to road
    var service = new google.maps.DirectionsService(),polys,snap_path=[];               
    polys.setMap(map);
    placeMarker(myCoord[0], map);
    for(j=0;j<myCoord.length-1;j++){            
            service.route({origin: myCoord[j],destination: myCoord[j+1],travelMode: google.maps.DirectionsTravelMode.DRIVING},function(result, status) {                
                if(status == google.maps.DirectionsStatus.OK) {                 
                      snap_path = snap_path.concat(result.routes[0].overview_path);
                      polys.setPath(snap_path);
                }        
            });
    }

I try to make a polyline snap to road from given marker points. My problem is, that the same code sometimes gives the good results, like in this imageenter image description here

and sometimes a bad result, like this: enter image description here

Any ideas why this is hapenning? And also, is there a limitation for polyline snap to road?

My map ini code:

var myLatlng = new google.maps.LatLng(47.6557, 23.5833);
var mapOptions = {
    zoom: 14,
    minZoom: 13,
    maxZoom: 19,
    center: myLatlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    disableDefaultUI:   true,
    overviewMapControl: false,
    streetViewControl:  false,
    scaleControl:       false,
    mapTypeControl:     false,
    panControl:         true,
    panControlOptions:{
        position: google.maps.ControlPosition.TOP_RIGHT
    },
    zoomControl: true,
    zoomControlOptions: {
        style: google.maps.ZoomControlStyle.LARGE,
        position: google.maps.ControlPosition.TOP_RIGHT
    }
}
var map = new google.maps.Map(document.getElementById("map"), mapOptions);

My polyline route snap code:

var polys = new google.maps.Polyline({
                map: map,
                strokeColor: "#5555FF"
            });
    myCoord = [
                        new google.maps.LatLng(47.663383463156144, 23.58100461977301),
                        new google.maps.LatLng(47.659221287827435, 23.586240291770082),
                        new google.maps.LatLng(47.65534785438211, 23.576713085349184),
                        new google.maps.LatLng(47.66020405359421, 23.572249889548402)
            ]; 

    // BEGIN: Snap to road
    var service = new google.maps.DirectionsService(),polys,snap_path=[];               
    polys.setMap(map);
    placeMarker(myCoord[0], map);
    for(j=0;j<myCoord.length-1;j++){            
            service.route({origin: myCoord[j],destination: myCoord[j+1],travelMode: google.maps.DirectionsTravelMode.DRIVING},function(result, status) {                
                if(status == google.maps.DirectionsStatus.OK) {                 
                      snap_path = snap_path.concat(result.routes[0].overview_path);
                      polys.setPath(snap_path);
                }        
            });
    }

原文:https://stackoverflow.com/questions/15925947
更新时间:2023-05-02 08:05

最满意答案

您可以实现一个将bool转换为FontWeight的IValueConverter,并将其用作绑定的转换器:

<UserControl.Resources>
    <local:BoolToFontWeightConverter x:Key="boolToFontWeight"/>
</UserControl.Resources>

...

<TextBlock Text="{Binding Text}" FontWeight="{Binding IsDefault, Converter={StaticResource boolToFontWeight}}">

You could implement a IValueConverter that converts a bool to a FontWeight, and use it as the binding's converter :

<UserControl.Resources>
    <local:BoolToFontWeightConverter x:Key="boolToFontWeight"/>
</UserControl.Resources>

...

<TextBlock Text="{Binding Text}" FontWeight="{Binding IsDefault, Converter={StaticResource boolToFontWeight}}">

相关问答

更多
  • 以下所有内容在SL4中都不起作用,因为它取决于Setter.Value绑定。 尝试在ItemContainerStyle设置绑定,因为您的StackPanel不是根元素; 您的模板将被放置在ContentPresenter ,因此您在StackPanel画布定位附加属性将被忽略。