Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.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
谷歌地图方向服务地理位置和地址输入jquery mobile_Jquery_Google Maps_Jquery Mobile_Google Maps Api 3 - Fatal编程技术网

谷歌地图方向服务地理位置和地址输入jquery mobile

谷歌地图方向服务地理位置和地址输入jquery mobile,jquery,google-maps,jquery-mobile,google-maps-api-3,Jquery,Google Maps,Jquery Mobile,Google Maps Api 3,我制作了一个简单的jquery移动地图,它使用谷歌地图和方向服务。这一切都是可行的,除非用户输入地址(起始地址或目的地址),而不输入城市,否则结果会非常遥远。Google directions将返回另一个省份甚至美国的某个地区 我认为方向将基于lat长原点,它将尝试使用本地地址地理编码?如何改进输入,以便用户只需输入地址即可获得更准确的结果 下面是我的代码: $(document).on("pageinit", "#map_page", function () { in

我制作了一个简单的jquery移动地图,它使用谷歌地图和方向服务。这一切都是可行的,除非用户输入地址(起始地址或目的地址),而不输入城市,否则结果会非常遥远。Google directions将返回另一个省份甚至美国的某个地区

我认为方向将基于lat长原点,它将尝试使用本地地址地理编码?如何改进输入,以便用户只需输入地址即可获得更准确的结果

下面是我的代码:

 $(document).on("pageinit", "#map_page", function () {
            initialize();
        });

        $(document).on('click', '#getDirectionsSubmit', function (e) {
            e.preventDefault();
            calculateRoute();
        });

        $(document).on('click', '#getCurrentLoc', function (e) {
            e.preventDefault();
            findCurrentPosition();
        });

        var directionDisplay,
            directionsService = new google.maps.DirectionsService(),
            map;
        var geocoder = new google.maps.Geocoder();

        function initialize() {
            // set the default center of the map
            var mapCenter = new google.maps.LatLng(55.1669513, -118.8031093);
            // set route options (draggable means you can alter/drag the route in the map)
            var rendererOptions = { draggable: true };
            directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions);

            //updateMapSize(mapCenter);
            // set the display options for the map
            var myOptions = {
                mapTypeControl: false,
                zoom: 12,
                mapTypeId: google.maps.MapTypeId.ROADMAP,
                center: mapCenter
            }
            // add the map to the map placeholder
            map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

            // bind the map to the directions
            directionsDisplay.setMap(map);
            // point the directions to the container for the direction details
            directionsDisplay.setPanel(document.getElementById("directionsPanel"));         

        }

        function notFound(msg) {
            alert('Could not find your location :(')
        }

        function findCurrentPosition() {   
            // start the geolocation API
            if (navigator.geolocation) {
                // when geolocation is available on your device, run this function
                navigator.geolocation.getCurrentPosition(foundYou, notFound);
            } else {
                // when no geolocation is available, alert this message
                alert('Geolocation not supported or not enabled.');
            }
        }

        function foundYou(position) {
            // convert the position returned by the geolocation API to a google coordinate object
            var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
            // then try to reverse geocode the location to return a human-readable address
            geocoder.geocode({ 'latLng': latlng }, function (results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    // if the geolocation was recognized and an address was found
                    if (results[0]) {
                        // add a marker to the map on the geolocated point
                        marker = new google.maps.Marker({
                            position: latlng,                   
                            map: map
                        });
                        // compose a string with the address parts
                        var address = results[0].address_components[1].long_name + ' ' + results[0].address_components[0].long_name + ', ' + results[0].address_components[3].long_name
                        // set the located address to the link, show the link and add a click event handler

                            // onclick, set the geocoded address to the start-point formfield
                            $('#from').text(address);
                            $('#from').val(address);
                            // call the calcRoute function to start calculating the route        
                    }
                } else {
                    // if the address couldn't be determined, alert and error with the status message
                    alert("Geocoder failed due to: " + status);
                }
            });
        }

        function calculateRoute() {
            var selectedtravelMode = $('#mode option:selected').val();
            // alert(selectedtravelMode);
            start = $("#from").val();
                end = $("#to").val();

            if (start == '' || end == '') {
                // cannot calculate route
                $("#results").hide();
                return;
            }
            else {
                var request = {
                    origin: start,
                    destination: end,                    
                    travelMode: google.maps.DirectionsTravelMode[selectedtravelMode]
                };

                directionsService.route(request, function (response, status) {
                    if (status == google.maps.DirectionsStatus.OK) {
                        directionsDisplay.setDirections(response);
                        $("#results").show();                   
                    }
                    else {
                        $("#results").hide();
                    }
                });

            }
        }
    </script>
</head>
<body>
    <div data-role="page" id="map_page">
        <div data-role="header">
            <h1>Directions Map</h1>
            <a href="http://mysiteaddress/" data-ajax="false" rel="external" id="returnMobile">Home</a>
        </div>
        <div data-role="content" class="ui-content">
            <div>
                <div id="map_canvas" style="width: 100%; height: 300px"></div>
                <div data-role="my-ui-field-contain">
                    <!--<div>
            <label for="mode" class="select">Transportation method:</label>
        </div>-->
                    <div>
                        <select name="select-choice-0" id="mode">
                            <option value="TRANSIT">Public Transit</option>
                            <option value="DRIVING">Driving</option>
                            <option value="WALKING">Walking</option>
                            <option value="BICYCLING">Bicycling</option>
                        </select>
                    </div>
                </div>                
                <div data-role="my-ui-field-contain">
                    <input type="text" id="from" placeholder="From Address" value="" /><button id="getCurrentLoc" data-icon="star">Use Current Location</button>
                </div>
                <div data-role="my-ui-field-contain">
                    <input type="text" id="to" placeholder="To Destination" value="" />
                </div>
                <a data-icon="search" data-role="button" href="#" id="getDirectionsSubmit">Get directions</a>
            </div>
            <div id="results" style="display:none;">
                <div id="directionsPanel"></div>
            </div>
        </div>
    </div>
</body>
</html>
$(document).on(“pageinit”、“#map_page”,函数(){
初始化();
});
$(文档)。在('单击','获取方向提交')上,函数(e){
e、 预防默认值();
计算器输出();
});
$(文档).on('click','getCurrentLoc',函数(e){
e、 预防默认值();
findCurrentPosition();
});
变量方向显示,
directionsService=新建google.maps.directionsService(),
地图
var geocoder=new google.maps.geocoder();
函数初始化(){
//设置地图的默认中心
var mapCenter=new google.maps.LatLng(55.1669513,-118.80301093);
//设置路线选项(可拖动意味着您可以在地图中更改/拖动路线)
var renderoptions={draggable:true};
directionsDisplay=新建google.maps.DirectionsRenderer(渲染器选项);
//更新地图(地图中心);
//设置地图的显示选项
变量myOptions={
mapTypeControl:false,
缩放:12,
mapTypeId:google.maps.mapTypeId.ROADMAP,
中心:地图中心
}
//将地图添加到地图占位符
map=new google.maps.map(document.getElementById(“map_canvas”),myOptions);
//将地图与方向绑定
方向显示.setMap(地图);
//将方向指向容器以获取方向详细信息
directionsDisplay.setPanel(document.getElementById(“directionsPanel”);
}
未找到函数(msg){
警报('找不到您的位置:(')
}
函数findCurrentPosition(){
//启动地理定位API
if(导航器.地理位置){
//当设备上的地理位置可用时,运行此功能
navigator.geolocation.getCurrentPosition(foundYou,notFound);
}否则{
//当没有可用的地理位置时,警告此消息
警报(“地理位置不受支持或未启用”);
}
}
职能部门(职位){
//将地理定位API返回的位置转换为google坐标对象
var latlng=新的google.maps.latlng(position.coords.latitude,position.coords.longitude);
//然后尝试对该位置进行反向地理编码,以返回人类可读的地址
geocoder.geocode({'latLng':latLng},函数(结果,状态){
if(status==google.maps.GeocoderStatus.OK){
//如果已识别地理位置并找到地址
如果(结果[0]){
//在地理定位点上向地图添加标记
marker=新的google.maps.marker({
位置:latlng,
地图:地图
});
//用地址部分组成一个字符串
var address=results[0]。address\u components[1]。long\u name+''+results[0]。address\u components[0]。long\u name+,'+results[0]。address\u components[3]。long\u name
//设置链接的定位地址,显示链接并添加单击事件处理程序
//单击后,将地理编码地址设置为起始点formfield
$('#from')。文本(地址);
$('#from').val(地址);
//调用calcRoute函数开始计算路线
}
}否则{
//如果无法确定地址,请使用状态消息发出警报并出错
警报(“地理编码器因“+状态”而失败);
}
});
}
函数计算器输出(){
var selectedtravelMode=$(“#模式选项:selected”).val();
//警报(选择行程模式);
start=$(“#from”).val();
end=$(“#到”).val();
如果(开始=“”| |结束=“”){
//无法计算路线
$(“#结果”).hide();
回来
}
否则{
var请求={
来源:start,
目的地:完,
travelMode:google.maps.DirectionsTravelMode[selectedtravelMode]
};
路由(请求、功能(响应、状态){
if(status==google.maps.directionstatus.OK){
方向显示。设置方向(响应);
$(“#结果”).show();
}
否则{
$(“#结果”).hide();
}
});
}
}
方向图
公共交通
德里维