【问题标题】:Display Marker on Custom Google Map在自定义谷歌地图上显示标记
【发布时间】:2014-05-14 10:40:30
【问题描述】:

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

【问题讨论】:

  • 新地图和自定义地图有什么区别? coords 是什么,你期待什么?
  • 自定义地图是我创建的地图。它突出显示了某条路线,并在某些点有标记。这是 url mapsengine.google.com/map/… 当前的 lat long 值存在于“coords”中。我想在“我的地图”上用标记显示这个位置。

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


【解决方案1】:

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

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

这里的coords[0] 和coords[1] 是浮点型纬度和经度值。

在此之后通过变量myLatlng 代替标记中的coords。

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2012-09-05
      • 2016-01-26
      • 2018-11-15
      • 1970-01-01
      • 1970-01-01
      • 2013-08-21
      相关资源
      最近更新 更多