在自定义Google地图上显示标记

时间:2014-05-14 10:40:30

标签: javascript jquery google-maps google-maps-api-3

我正在使用JQuery mobile,HTML5来开发实时车辆跟踪应用程序。我想知道如何将标记放在“自定义”的当前位置。地图。我使用getLocation函数来获取当前坐标。

var mapOptions={
zoom:15
mapTypeControl: true; 
navigationControlOptions:{style:google.maps.navigationControl.Style.Small}mapTypeID.ROADMAP};
map=google.maps.Map(document.getElementByID("map_container"),mapOptions);
 var marker = new google.maps.Marker({               
  position: coords,               
  map: map,                
  title: "You Are Here!"           
  });      
}

google.maps.Map函数会加载一个新的'地图。我想在自定义地图上显示标记。

1 个答案:

答案 0 :(得分:0)

这是我在项目中使用的一些代码:

function CurrentPosition(){
    getPosition(function(mylat, mylng){
        drawPosition(mylat, mylng);
    });
}

function getPosition(callback) {
    navigator.geolocation.getCurrentPosition(function(position){
        callback(position.coords.latitude, position.coords.longitude);
    }, function(){
        alert(texte['myPositionErrorAlert']);
    },{enableHighAccuracy: false, timeout: 20000, maximumAge: 0});
}

function drawPosition(mylat, mylng){
    LatLng = new google.maps.LatLng(mylat, mylng);
    yourMarker = new google.maps.Marker({
        position: LatLng,
        map: map,
        title:"MyPosition"
    });
    yourMarker.setMap(map);
}

强制使用CurrentPosition函数,getPosition()将定位您的位置,drawPosition()将创建标记。

相关问题