显示标记自定义谷歌地图

Display Marker on Custom Google Map

本文关键字:谷歌地图 自定义 显示      更新时间:2023-09-26

我正在使用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函数加载一个"新"地图。我想在自定义地图上显示该标记。

您需要在将其传递给标记之前将这些坐标加载到LatLng对象中。如

var myLatlng = new google.maps.LatLng(coords[0], coords[1]);

其中coords[0]coords[1]为浮动型经纬度值。

在此之后传递变量myLatlng代替标记中的coords

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

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()将创建标记