dttl3933 2015-12-17 20:51
浏览 28

如果在某些里程内,显示数据库中的行? [关闭]

I am unsure how to start this,

Basically in my mysql database i have a table called "taxi" with the columns "company, base, miles" So the company one is Company name, Base is their main office postcode only, miles is how far they go to pick someone up?

Ideally want to use google maps, Will have a text box with id="start", so would need to extract that value (Their pickup postcode) and compare it with all the companys bases? and show which ones could pick up?

Forgot to mention i am trying do this in PHP Sorry if this does not make sense.

Thanks :)

  • 写回答

1条回答 默认 最新

  • doumao8355 2015-12-17 21:05
    关注
    function getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2) {
      var R = 6371; // Radius of the earth in km
      var dLat = deg2rad(lat2-lat1);  // deg2rad below
      var dLon = deg2rad(lon2-lon1); 
      var a = 
        Math.sin(dLat/2) * Math.sin(dLat/2) +
        Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * 
        Math.sin(dLon/2) * Math.sin(dLon/2)
        ; 
      var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
      var d = R * c; // Distance in km
      return d;
    }
    
    function deg2rad(deg) {
      return deg * (Math.PI/180)
    }
    

    I use the Above functions in my code block which is more less same logic as yours. get latlong of the postal code/address from google map api and get the latlong of your company bases and find the nearest one. You can also make use of the below query.

     SELECT *,
    
    
    3956 * 2 * ASIN(SQRT( POWER(SIN((@orig_lat -
    abs( 
    dest.lat)) * pi()/180 / 2),2) + COS(@orig_lat * pi()/180 ) * COS( 
    abs
    (dest.lat) *  pi()/180) * POWER(SIN((@orig_lon – dest.lon) *  pi()/180 / 2), 2) ))
    
    as distanceFROM hotels desthaving distance < @distORDER BY distance limit 10;
    
    评论

报告相同问题?