Javascript 显示当前位置+;使用谷歌地图API和phonegap的方向?
我有以下要求Javascript 显示当前位置+;使用谷歌地图API和phonegap的方向?,javascript,jquery,google-maps,cordova,google-maps-api-3,Javascript,Jquery,Google Maps,Cordova,Google Maps Api 3,我有以下要求 显示从起点到目的地的方向 显示用户的当前位置 我正在创建一个带有方向的地图,然后在上面显示用户的当前位置 function showDirection(orgn, dstntn) { var directionsService = new google.maps.DirectionsService(); var directionsDisplay = new google.maps.DirectionsRenderer(); var map = new g
function showDirection(orgn, dstntn)
{
var directionsService = new google.maps.DirectionsService();
var directionsDisplay = new google.maps.DirectionsRenderer();
var map = new google.maps.Map(document.getElementById('displayMap'), {
zoom : 7,
mapTypeId : google.maps.MapTypeId.ROADMAP
});
directionsDisplay.setMap(map);
directionsDisplay.setPanel(document.getElementById('displayDirections'));
var request = {
origin : orgn,
destination : dstntn,
travelMode : google.maps.DirectionsTravelMode.WALKING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});
var win = function(position) {
var lat = position.coords.latitude;
var long = position.coords.longitude;
var myLatlng = new google.maps.LatLng(lat, long);
var iconimage="images/current_location_small.png";
var marker = new google.maps.Marker({
position: myLatlng,
map: map
icon: iconimage
});
marker.setMap(map);
};
var fail = function(e) {
alert('Can\'t retrieve position.\nError: ' + e);
};
var watchID = navigator.geolocation.getCurrentPosition(win, fail);
}
如果用户位于为显示方向而创建的地图内,则上述代码可以正常工作,但如果用户不在地图区域内,则不会显示其当前位置
我想要所有这三点。起源,2。目的地和目的地3。用户在地图上的位置。是否有一种方法可以缩小地图以适应所有三个点,或者以所有三个点都可见的方式创建原始地图。在
marker.setMap(地图)之后添加以下内容代码>
marker.setMap(map);
//Add these lines to include all 3 points in the current viewport
var bounds = new google.maps.LatLngBounds();
bounds.extend(orgn);
bounds.extend(dstntn);
bounds.extend(myLatlng);
map.fitBounds(bounds);