日期:2014-05-16 浏览次数:20380 次
var newMap = {a:{},b:{name:"目的地"}}; //全局变量
newMap.directionsDisplay = {};
newMap.directionsService = new google.maps.DirectionsService(); //这两个是在导航的时候用到的
//初始化地图
function init_Map(mapCenter) {
newMap.directionsDisplay = new google.maps.DirectionsRenderer();
var myOptions = {
zoom:16,
mapTypeId: google.maps.MapTypeId.ROADMAP, //地图类型
center: mapCenter //LatLng 对象
}
newMap.map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
newMap.directionsDisplay.setMap(newMap.map);
}
//导航方法
function My_calcRoute(start,end) {
var request = {
origin:start, //开始的位置 (A)
destination:end, //开始的位置 (B)
travelMode: google.maps.TravelMode.DRIVING //导航类型 驾驶
};
newMap.directionsService.route(request, function(result, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(result);
}
});
}
function getMyLatLng(){
if( navigator.geolocation ) {
function gpsSuccess(pos){
if( pos.coords ){
newMap.a.lat = pos.coords.latitude;
newMap.a.lng = pos.coords.longitude;
}
else{
newMap.a.lat = pos.latitude;
newMap.a.lng = pos.longitude;
}
var userPositon = new google.maps.LatLng(newMap.a.lat,newMap.a.lng);
var crsPositon = new google.maps.LatLng("22.81366","108.34083");
init_Map(userPositon);
My_calcRoute(userPositon,crsPositon);
addMarker(crsPositon,newMap.map,newMap.b.name);
&nb