addr2geo.sh 1011 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. if [[ ${#@} -gt 0 ]]; then
  17. ADDRESS=$(echo -e "$@") # support hex code
  18. else
  19. echo "Please provide one address (for example: \"广州塔\")."
  20. exit 1
  21. fi
  22. SEARCH()
  23. {
  24. QUERY="$@"
  25. QUERY=${QUERY// /+}
  26. SEARCH_RESULT=`$DOWNLOAD "http://api.map.baidu.com/?qt=s&rn=1&wd=${QUERY}"`
  27. }
  28. SEARCH $ADDRESS
  29. while [[ ! $SEARCH_RESULT == *\"content\"* ]] && [[ ${#ADDRESS} -gt 1 ]]; do
  30. ADDRESS=${ADDRESS%?}
  31. SEARCH $ADDRESS
  32. done
  33. CONTENT_POS=${SEARCH_RESULT%%\"content\"*}
  34. CONTENT=${SEARCH_RESULT:${#CONTENT_POS}}
  35. FIRST_GEO_POS=${CONTENT%%\"geo\"*}
  36. FIRST_GEO=$(echo ${CONTENT:${#FIRST_GEO_POS}} | sed 's/"geo":"\([^"]*\)".*/\1/')
  37. echo $FIRST_GEO