简体   繁体   English

如何获取标记的像素坐标

[英]how to obtain marker's pixel coordinates

My code puts a marker on the map each time I click on it.每次单击时,我的代码都会在 map 上放置一个标记。 The objective is to get each time marker's lat/lon coordinates together with pixel coordinates.目标是获取每个时间标记的纬度/经度坐标和像素坐标。 So far I've been successful only in getting lat/lon coord.到目前为止,我只在获得纬度/经度坐标方面取得了成功。 The next step now would be taking these as input and compute the pixel coordinates.现在下一步是将这些作为输入并计算像素坐标。

<script>
    function initMap() {41.85, -87.65
    var myLatlng = {lat: 41.85, lng: -87.65};
    var map = new google.maps.Map(
          document.getElementById('map'), {zoom: 18,
                                           center: myLatlng,
                                           disableDefaultUI: false,
                                           mapTypeId: 'satellite',
                                           zoomControl: true,
                                           mapTypeControl: true,
                                           scaleControl: true,
                                           streetViewControl: false,
                                           rotateControl: false,
                                           fullscreenControl: true});

    map.setOptions({draggableCursor:'default'});
    map.addListener('click', function(marker){

    marker = new google.maps.Marker({map: map,
                                     clickable: false,
                                     position: marker.latLng,
                                     })

    var markerposLat = marker.getPosition().lat();
    var markerposLon = marker.getPosition().lng();
    
     function pixl(markerposLat,markerposLon){
       var projection = map.getProjection();
       var bounds = map.getBounds();
       var topRight = projection.fromLatLngToPoint(bounds.getNorthEast());
       var bottomLeft = projection.fromLatLngToPoint(bounds.getSouthWest());
       var scale = Math.pow(2, map.getZoom());
       var worldPoint = projection.fromLatLngToPoint(markerposLat,markerposLon);
       return [Math.floor((worldPoint.x - bottomLeft.x) * scale), Math.floor((worldPoint.y - topRight.y) * scale)]
    };

    localStorage["pixl"] = JSON.stringify(pixl);
    localStorage["markerLat"] = JSON.stringify(markerposLat);
    localStorage["markerLon"] = JSON.stringify(markerposLon);
    console.log(localStorage["pixl"],localStorage["markerLat"], localStorage["markerLon"]);
  });

}
</script>

Function pixl is always undefined. Function pixl 始终未定义。 I realize it's a question that have been asked many times.我意识到这是一个被问过很多次的问题。 In fact I've tried to adapt many methods.事实上,我尝试了很多方法。 My starting points are this: convert-lat-lon-to-pixels-and-back and of course this: showing pixel and tile coordinates .我的出发点是: convert-lat-lon-to-pixels-and-back ,当然还有: 显示像素和平铺坐标 I can't spot the problem.我无法发现问题。

Please note that the fromLatLngToPoint method requires a google.maps.LatLng class as its parameter.请注意, fromLatLngToPoint方法需要google.maps.LatLng class 作为其参数。 From the documentation :文档中:

fromLatLngToPoint(latLng[, point])从LatLngToPoint(latLng [,点])

Parameters:参数:
latLng: LatLng latLng: 纬度
point: Point optional点:点可选

Return Value: Point optional返回值:点可选

Translates from the LatLng cylinder to the Point plane.从 LatLng 圆柱体平移到 Point 平面。 This interface specifies a function which implements translation from given LatLng values to world coordinates on the map projection.此接口指定 function 实现从给定 LatLng 值到 map 投影上的世界坐标的转换。 The Maps API calls this method when it needs to plot locations on screen.地图 API 在需要 plot 屏幕上的位置时调用此方法。 Projection objects must implement this method, but may return null if the projection cannot calculate the Point.投影对象必须实现此方法,但如果投影无法计算点,则可能返回 null。

So in your code, I would do it this way instead:所以在你的代码中,我会这样做:

var worldPoint = projection.fromLatLngToPoint(marker.getPosition());

Another thing I (and @geocodezip) noticed is that you are not passing a parameter to your pixl function.我(和@geocodezip)注意到的另一件事是您没有将参数传递给您的pixl function。 This is why it is intended for you to get an undefined response.这就是为什么它旨在让您获得undefined的响应。 You should include a parameter like below instead in order to get the correct value:为了获得正确的值,您应该包含如下参数:

localStorage["pixl"] = JSON.stringify(pixl((markerposLat,markerposLon)));

Here is the working fiddle for this.这是为此工作的小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM