a failed attempt at placing a dot over an image based on known lat and lng points

主宰稳场 提交于 2019-12-06 13:33:36

Assuming the area is a parallelogram, you'll need to know 3 of the vertices of the area and the width/height of the area where you want to draw the pin(e.g. the floorplan-image).

The following will use the Geo-and LatLon-libraries from http://www.movable-type.co.uk/scripts/latlong.html

An image for better understanding:

initially calculate some values:

  • bearings(nw to sw and nw to ne) via LatLon.bearingTo
  • distances(sw to nw and ne to nw) via LatLon.distanceTo

now calculate the intersections(marked as intersection-x and intersection-y in the image) via LatLon.intersection

The calculation would be:

  • intersection-x:
    LatLon.intersection(ne, bearingNWtoSW, target, bearingNWtoNE)
  • intersection-y:
    LatLon.intersection(sw, bearingNWtoNE, target,bearingNWtoSW)

now calculate percentual values for the distance of border-north to target and border-west to target:

  • border-north to target:
    ((distanceNWtoNE-(target.distanceTo(intersection-x)))*100)/distanceNWtoNE
  • border-west to target:
    ((distanceNWtoSW-(target.distanceTo(intersection-y)))*100)/distanceNWtoSW

Finally calculate the coordinates based on the width/height of the given floorplan and the results of the previous calculation

x=((width*distanceBorderNorthToTargetInPercent)/100);
y=((height*distanceBorderWestToTargetInPercent)/100);

A method(extension of the mentioned LatLon-library) that performs all these calculations:

/**
  *@param w int width of the drawing
  *@param h int height of the drawing
  *@param sw object LatLon of southwest of the drawing     
  *@param nw object LatLon of northwest of the drawing     
  *@param ne object LatLon of northeast of the drawing
  *@return mixed object with x/y-coordinates or null when LatLon is outside of the area        
  **/  
LatLon.prototype.translate = function(w,h,sw,nw,ne) {
    var x = {distance:nw.distanceTo(ne),bearing:nw.bearingTo(ne)},
        y = {distance:nw.distanceTo(sw),bearing:nw.bearingTo(sw)},
        intersectionY = LatLon.intersection(sw, x.bearing, this, y.bearing),
        intersectionX = LatLon.intersection(ne, y.bearing, this, x.bearing),
        distanceX,distanceY; 

    if(intersectionX && intersectionY){
       distanceX=((x.distance-(this.distanceTo(intersectionX)))*100)/x.distance,
       distanceY=((y.distance-(this.distanceTo(intersectionY)))*100)/y.distance;
       return {x:((w*distanceX)/100),y:((h*distanceY)/100)};
    }
    return null;
};

Demo:http://jsfiddle.net/doktormolle/nsbqpcvg/embedded/result/
Hover the highlighted floorplan on the google-map to see the result of the calculation(the google-Maps-API is only used for the demonstration, the calculation will be done without using the Maps-API)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!