Get longtitude latitude combination based on distance and degree

感情迁移 提交于 2020-01-01 03:42:07

问题


I am trying to get geo combinations of a grid which is covered a certain area (My city). The grid is 500m * 500m. So I wanna enter starting long/latitude combination,distance (500m), degree (90 or 270) and get the next long/latitude combination.

I am so unaware of mathematical formulas, and hard to find a solution. I had found some php functions that return distance in between two long/latitude combinations.

http://www.zipcodeworld.com/samples/distance.php.html http://www.phpro.org/tutorials/Geo-Targetting-With-PHP-And-MySQL.html#3

But still I cannot rebuild this thing according to my need (I really hate cos,sin,tan). Really appreciate if someone help me over this.


回答1:


Here is the code for finding four extreme points (lat, long) around a center point

 <script type="text/javascript">
            function toRad (value) {
                return value * Math.PI / 180;
            }

            function toDeg (value) {
                return value * 180 / Math.PI;
            }
            function computePoint(){                
                var radius = 6371;  //in kms
                var lat1 = checkField(document.getElementById("lat"));
                var lon1 = checkField(document.getElementById("long"));
                var dist = document.getElementById("distance").value;

                dist = typeof(dist)=='number' ? dist : typeof(dist)=='string' && dist.trim()!='' ? +dist : NaN;
                //                alert("Dist "+ dist+" kms");
                var angularDist = dist / radius;
                lat1= toRad(lat1);
                lon1=toRad(lon1);

                var lat2,lon2, angle = 0;

                for(i=1; i<5; i++,angle+=90){                  
                    var brng = toRad(angle);
                    lat2=lon2=0;
                    // second latitude
                    lat2 = Math.asin(Math.sin(lat1) * Math.cos(angularDist) + 
                        Math.cos(lat1) * Math.sin(angularDist) * Math.cos(brng));

                    // second longitude
                    lon2 = lon1 + Math.atan2(Math.sin(brng) * Math.sin(angularDist) *
                        Math.cos(lat1), Math.cos(angularDist) - Math.sin(lat1) * Math.sin(lat2));

                    if (isNaN(lat2) || isNaN(lon2)) alert("Something is null");

                    //                lon2 = (lon2+3*Math.PI) % (2*Math.PI) - Math.PI;  // normalise to -180..+180º
                    lat2 = toDeg(lat2);
                    lon2 = toDeg(lon2);
//                    alert("latitude: "+lat2 +",Longitude "+lon2);
                    document.getElementById("lat"+i).innerHTML=lat2;
                    document.getElementById("long"+i).innerHTML=lon2;
                    document.getElementById("ang"+i).innerHTML=angle;
                }

            }
            function checkField(field){
                var str=field.name
                var latlon1
                latlon=field.value;//parselatlon(field.value)
                if (str.substring(0,3)=="lat") {
                    if (latlon > 90.) {
                        alert ("Latitudes cannot exceed 90 degrees")
                        field.focus()  // Doesn't work!
                        field.select()
                    }
                }
                if (str.substring(0,3)=="long") {
                    if (latlon > 180.) {
                        alert ("Longitudes cannot exceed 180 degrees")
                        field.focus()
                        field.select()      
                    }
                }
                return latlon
            }

            function parselatlon(instr){
                // Parse strings dd.dd dd:mm.mm dd:mm:ss.ss
                var deg,min,sec,colonIndex,degstr,minstr,str
                str=instr
                colonIndex=str.indexOf(":")
                if (colonIndex==-1){ // dd.dd?
                    if (!isPosNumber(str)){
                        badLLFormat(instr)
                        return 0.
                    } else {
                        return parseFloat(str)
                    }
                }
            }
            function isPosNumber(instr){ //integer or float
                str=""+instr // force to string type
                oneDecimal=false
                for (var i=0;i<str.length;i++) {
                    var oneChar=str.charAt(i)
                    if (oneChar=="." && !oneDecimal){
                        oneDecimal=true
                        continue
                    }
                    if (oneChar < "0" || oneChar > "9") {
                        return false
                    }
                }
                return true
            }
            function badLLFormat(str){
                alert(str+ " is an invalid lat/lon format\n"+
                    "Use DD.DD DD:MM.MM or DD:MM:SS.SS")
            }
        </script>

and HTML looks like ....

<body>
        <div>Enter the details </div>
        Latitude  : <input type="text" id="lat" name="lat" value="" /> <br/>
        Longitude : <input type="text" id="long" name="long" value="" /> <br/>
        <!--        Bearing   : <input type="text" id="bearing" name="bearing" value="" placeholder="Enter angle in degrees" /> <br/>-->
        Distance  : <input type="text" id="distance" name="distance" value="" placeholder ="Enter distance in Kms. " /> <br/> 
        <input type="submit" value="Find Points" name="findPts" onclick="computePoint();"/>
        <div><h3><span> <strong>Results Will be displayed here</strong></span></h3></div>            
        <table border="1" style="text-align: center">
            <tr>
                <th>Latitude</th>
                <th>Longitude</th>
                <th>Angle (in degrees) </th>
            </tr>
            <tr>
                <td><label id="lat1" value=""></label> </td>
                <td><label id="long1" value=""></label> </td>
                <td><label id="ang1" value=""></label> </td>
            </tr>
            <tr>
                <td><label id="lat2" value=""></label> </td>
                <td><label id="long2" value=""></label> </td>
                <td><label id="ang2" value=""></label> </td>
            </tr>
            <tr>
                <td><label id="lat3" value=""></label> </td>
                <td><label id="long3" value=""></label> </td>
                <td><label id="ang3" value=""></label> </td>
            </tr>
            <tr>
                <td><label id="lat4" value=""></label> </td>
                <td><label id="long4" value=""></label> </td>
                <td><label id="ang4" value=""></label> </td>
            </tr>
        </table>
    </body>

I here by donate my code under GPL.... enjoy!!




回答2:


This site has example code for many lat/long/distance/bearing calculations:

http://www.movable-type.co.uk/scripts/latlong.html

The formula are:

lat2 = asin(sin(lat1)*cos(d/R) + cos(lat1)*sin(d/R)*cos(θ))

lon2 = lon1 + atan2(sin(θ)*sin(d/R)*cos(lat1), cos(d/R)−sin(lat1)*sin(lat2))

θ is the bearing (in radians, clockwise from north);

d/R is the angular distance (in radians), where d is the distance travelled and R is the earth’s radius

The relevant code you are interested in is in javascript, but should be easy to convert:

var lat2 = Math.asin( Math.sin(lat1)*Math.cos(d/R) + 
                      Math.cos(lat1)*Math.sin(d/R)*Math.cos(brng) );
var lon2 = lon1 + Math.atan2(Math.sin(brng)*Math.sin(d/R)*Math.cos(lat1), 
                             Math.cos(d/R)-Math.sin(lat1)*Math.sin(lat2));


来源:https://stackoverflow.com/questions/8800267/get-longtitude-latitude-combination-based-on-distance-and-degree

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