point2addr.sh 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #!/bin/bash
  2. # Baidu Maps Coordinates Utils
  3. # https://github.com/caiguanhao/baidu-maps-coord-utils
  4. WGET=$(which wget)
  5. CURL=$(which curl)
  6. if [[ ${#WGET} -eq 0 ]]; then
  7. if [[ ${#CURL} -eq 0 ]]; then
  8. echo "Install wget or curl first."
  9. exit 1
  10. else
  11. DOWNLOAD="$CURL -G -L -s"
  12. fi
  13. else
  14. DOWNLOAD="$WGET --quiet -O -"
  15. fi
  16. ARG_1=${1//,/}
  17. ARG_2=$2
  18. if [[ $ARG_1 =~ ^[+-]?[0-9]*\.?[0-9]+$ ]] &&
  19. [[ $ARG_2 =~ ^[+-]?[0-9]*\.?[0-9]+$ ]]
  20. then
  21. POINT_X=$ARG_1
  22. POINT_Y=$ARG_2
  23. else
  24. echo "Please provide one point (for example: 12616224.86, 2628601.01)."
  25. exit 1
  26. fi
  27. API="http://api.map.baidu.com/"
  28. RESULT=`$DOWNLOAD "${API}?qt=rgc&x=${POINT_X}&y=${POINT_Y}"`
  29. if [[ ! $RESULT == *\"content\"* ]]; then
  30. echo "Address not found."
  31. exit 1
  32. fi
  33. CONTENT_POS=${RESULT%%\"content\"*}
  34. CONTENT=${RESULT:${#CONTENT_POS}}
  35. ADDRESS_POS=${CONTENT%%\"address\"*}
  36. ADDRESS=$(echo ${CONTENT:${#ADDRESS_POS}} | sed 's/"address":"\([^"]*\)".*/\1/')
  37. if [[ ${#ADDRESS} -eq 0 ]]; then
  38. echo "Address not found."
  39. exit 1
  40. else
  41. echo $ADDRESS
  42. fi