baidu_geocoder 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d722d967cf11d72af495bb81301b318045ca8cd3
4
+ data.tar.gz: 6f7a54198b335b78c4bee3e964d5425e6068a1fd
5
+ SHA512:
6
+ metadata.gz: 6802c79c7287f8de4099d62b2be54b4e6125a35c75b64f791ad436d9e250b148f1b08df98ac4236fa17b4dea6a2835a420fe8b26aa793beacb6759346c23c42c
7
+ data.tar.gz: 6d3ce63014ab52c1f500b2715b43b07fcafc441e8c86cc91b52aaf70934e30aa13748e3fe421f8edc0b07962c6a90fb0c54238c70489485435a29fe8c4b344df
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in baidu_geocoder.gemspec
4
+ gemspec
5
+ gem 'http'
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 RubyCat
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,59 @@
1
+ # BaiduGeocoder[![Gem Version](https://badge.fury.io/rb/baidu_geocoder.svg)](http://badge.fury.io/rb/baidu_geocoder)
2
+
3
+ 使用百度地图API进行地址定位坐标、坐标定位地址、名称定位地址。
4
+
5
+ ## 安装
6
+
7
+ $ gem install baidu_geocoder
8
+
9
+ ## 使用
10
+
11
+ ### 调用
12
+
13
+ ```ruby
14
+ require 'baidu_geocoder'
15
+ include 'BaiduGeocoder'
16
+
17
+ ## get_location_by_addr
18
+ loc = Geocoder.get_location_by_addr(addr)
19
+
20
+ ## get_address_by_name
21
+ addr = Geocoder.get_address_by_name(name)
22
+
23
+ ## get_address_by_loc
24
+ addr = Geocoder.get_address_by_loc(lat, lng)
25
+
26
+ ```
27
+
28
+ - tips: BaiduMapApi需要申请 MapAk. 为了避免因为ak限制问题,您也可以传入自己的ak([申请地址](http://lbsyun.baidu.com/apiconsole/key))
29
+
30
+ ```ruby
31
+ loc = Geocoder.get_location_by_addr(addr, your_ak)
32
+ ```
33
+
34
+ ### 命令行
35
+
36
+ ```
37
+ ➜ baidu_geocoder git:(master) ✗ baidu_geocoder
38
+ Usage:
39
+ baidu_geocoder -a addr : 返回坐标
40
+ baidu_geocoder -n name : 返回地址
41
+ baidu_geocoder -l lat lng : 返回地址
42
+ ➜ baidu_geocoder git:(master) ✗ baidu_geocoder -n 北京市政府
43
+ 获取地址成功:
44
+ {:province=>"北京市", :city=>"北京市", :district=>"西城区", :formatted_address=>"北京市西城区文津街"}
45
+
46
+ ➜ baidu_geocoder git:(master) ✗ baidu_geocoder -a 北京市西城区文津街
47
+ 获取定位成功:
48
+ {:lng=>116.39321980074757, :lat=>39.92881485241565}
49
+
50
+ ➜ baidu_geocoder git:(master) ✗ baidu_geocoder -l 39.92881485241565 116.39321980074757
51
+ 获取地址成功:
52
+ {:province=>"北京市", :city=>"北京市", :district=>"西城区", :formatted_address=>"北京市西城区文津街3号"}
53
+
54
+ ```
55
+
56
+ ## License
57
+
58
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
59
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'baidu_geocoder/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "baidu_geocoder"
8
+ spec.version = BaiduGeocoder::VERSION
9
+ spec.authors = ["RubyCat"]
10
+ spec.email = ["chenxueping1819@gmail.com"]
11
+
12
+ spec.summary = %q{Geocoder by Baidu Map}
13
+ spec.description = %q{Geocoder by Baidu Map}
14
+ spec.homepage = "https://github.com/a598799539/baidu_geocoder"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.11"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.0"
25
+ spec.add_development_dependency "http", "~> 2.0"
26
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "baidu_geocoder"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,56 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "baidu_geocoder"
4
+ include BaiduGeocoder
5
+
6
+ desc = <<-EOF
7
+ Usage:
8
+ baidu_geocoder -a addr : 返回坐标
9
+ baidu_geocoder -n name : 返回地址
10
+ baidu_geocoder -l lat lng : 返回地址
11
+ EOF
12
+
13
+ if ARGV && ARGV.size > 1
14
+ option = ARGV[ 0 ].dup
15
+ puts case option
16
+ when '-a'
17
+ addr = ARGV[ 1 ].dup
18
+ loc = Geocoder.get_location_by_addr(addr)
19
+ if loc[:status]
20
+ puts "获取定位成功:"
21
+ else
22
+ puts "获取定位失败:"
23
+ end
24
+ loc.delete(:status)
25
+ puts loc.to_s
26
+ when '-n'
27
+ name = ARGV[ 1 ].dup
28
+ addr = Geocoder.get_address_by_name(name)
29
+ if addr[:status]
30
+ puts "获取地址成功:"
31
+ else
32
+ puts "获取地址失败:"
33
+ end
34
+ addr.delete(:status)
35
+ puts addr.to_s
36
+ when '-l'
37
+ if ARGV.size > 2
38
+ lat = ARGV[ 1 ].dup
39
+ lng = ARGV[ 2 ].dup
40
+ addr = Geocoder.get_address_by_loc(lat, lng)
41
+ if addr[:status]
42
+ puts "获取地址成功:"
43
+ else
44
+ puts "获取地址失败:"
45
+ end
46
+ addr.delete(:status)
47
+ puts addr.to_s
48
+ else
49
+ puts desc
50
+ end
51
+ else
52
+ puts desc
53
+ end
54
+ else
55
+ puts desc
56
+ end
@@ -0,0 +1,76 @@
1
+ require "baidu_geocoder/version"
2
+ require 'http'
3
+
4
+ module BaiduGeocoder
5
+ class Geocoder
6
+ ApiHost = "http://api.map.baidu.com"
7
+ ApiAction = "/geocoder/v2/"
8
+ ApiUrl = ApiHost + ApiAction
9
+ DefaultAK = "LDw1AMLeanI9hEMjKG4DCzczYVaxAYFC"
10
+ DefaultLocation = {:lng => 116.421885, :lat => 39.938574, :status => false}.freeze
11
+ DefaultAddress = {:province => "北京市", :city => "市辖区", :district => "东城区", :formatted_address => "北京市市辖区东城区", :status => false}.freeze
12
+
13
+ class << self
14
+
15
+ ## get location by address
16
+ def get_location_by_addr(addr,city = nil, ak = DefaultAK, http = HTTP)
17
+ names = {'宁夏' => '宁夏回族自治区', '新疆'=> '新疆维吾尔自治区', '西藏'=> '西藏自治区',
18
+ '广西'=> '广西壮族自治区', '内蒙'=> '内蒙古自治区'}
19
+ names.each do |k,v|
20
+ addr.gsub!(/#{k}/, v)
21
+ end
22
+ params = {:address => addr, :city => city}.merge({:output => "json", :ak => ak})
23
+ loc = {}.merge(DefaultLocation)
24
+ begin
25
+ res = http.get(ApiUrl, :params => params).flush
26
+ if res.code == 200
27
+ json = JSON.parse(res.body)
28
+ if "0".eql?(json["status"].to_s) && json["result"] && !json["result"].empty? \
29
+ && json["result"]["location"] && !json["result"]["location"] .empty?
30
+ loc[:lng] = json["result"]["location"]["lng"]
31
+ loc[:lat] = json["result"]["location"]["lat"]
32
+ loc[:status] = true
33
+ end
34
+ end
35
+ rescue Exception => e
36
+ puts "Exception=> #{e}"
37
+ end
38
+ loc
39
+ end
40
+
41
+ ## get address by location(latitude, longtitude)
42
+ def get_address_by_loc(lat, lng, ak = DefaultAK, http = HTTP)
43
+ params = {:location => "#{lat},#{lng}"}.merge({:output => "json", :ak => ak})
44
+ address = {}.merge(DefaultAddress)
45
+ begin
46
+ res = http.get(ApiUrl, :params => params).flush
47
+
48
+ if res.code == 200
49
+ json = JSON.parse(res.body)
50
+ if "0".eql?(json["status"].to_s) && json["result"] && !json["result"].empty? \
51
+ && json["result"]["addressComponent"] && !json["result"]["addressComponent"].empty? \
52
+ && json["result"]["formatted_address"] && !json["result"]["formatted_address"].empty?
53
+ address[:formatted_address] = json["result"]["formatted_address"]
54
+ address[:province] = json["result"]["addressComponent"]["province"]
55
+ address[:city] = json["result"]["addressComponent"]["city"]
56
+ address[:district] = json["result"]["addressComponent"]["district"]
57
+ address[:status] = true
58
+ end
59
+ end
60
+ rescue Exception => e
61
+ puts "Exception => #{e}"
62
+ end
63
+
64
+ address
65
+ end
66
+
67
+ def get_address_by_name name, ak = DefaultAK
68
+ loc = get_location_by_addr(name, ak)
69
+ address = get_address_by_loc(loc[:lat], loc[:lng], ak)
70
+ address[:status] = loc[:status] && address[:status]
71
+ address
72
+ end
73
+ end
74
+ end
75
+
76
+ end
@@ -0,0 +1,3 @@
1
+ module BaiduGeocoder
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: baidu_geocoder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - RubyCat
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-07-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: http
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '2.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '2.0'
69
+ description: Geocoder by Baidu Map
70
+ email:
71
+ - chenxueping1819@gmail.com
72
+ executables:
73
+ - baidu_geocoder
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - .gitignore
78
+ - .rspec
79
+ - .travis.yml
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - baidu_geocoder.gemspec
85
+ - bin/console
86
+ - bin/setup
87
+ - exe/baidu_geocoder
88
+ - lib/baidu_geocoder.rb
89
+ - lib/baidu_geocoder/version.rb
90
+ homepage: https://github.com/a598799539/baidu_geocoder
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - '>='
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.4.8
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: Geocoder by Baidu Map
114
+ test_files: []
115
+ has_rdoc: