我想要使用Google Place API使用唯一的lat,lng坐标来确定地址详情。 我尝试了附近的搜索,文本搜索和雷达搜索,但它也需要半径。然后我尝试对地理编码API进行地理编码,但它提供了多个地方的详细信息,我想要一个地方的详细信息。
有没有办法实现这个目标?
答案 0 :(得分:0)
gem 'twitter'
创建控制器:
class Api::MapController < ApplicationController
def new
end
def show
client = Twitter::REST::Client.new do |config|
config.consumer_key = "your_consumer_key"
config.consumer_secret = "your_consumer_secret"
config.access_token = "your_access_token"
config.access_token_secret = "your_access_token_secret"
end
#center: {lat: -34.397, lng: 150.644},
latitude = params[:lat]
longitude = params[:lng]
#str = '31.515181,74.345034,10km'
str = "#{latitude.to_s},#{longitude.to_s},10km"
@search_results = client.search( '', geocode: str ).take(15)
render json: @search_results
end
end
如果需要1个结果,您可以设置0kM。并在client.search( 'Coffe Shops', geocode: str ).take(15)
方法
最后添加此Javascript添加并在Google地图上显示标记
computed_latitude = 150.644;
computed_longitude = -34.397;
map = null;
function initMap() {
// Try HTML5 geolocation.
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (position) {
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
computed_latitude = pos.lat;
computed_longitude = pos.lng;
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: computed_latitude, lng: computed_longitude},
zoom: 6
});
// fetch tweets
console.log('Fetching tweets now...');
$.ajax({
url: "http://localhost:3000/api/map/show", // Route to the Script Controller method
type: "GET",
dataType: "json",
data: {lat: computed_latitude, lng: computed_longitude}, // This goes to Controller in params hash, i.e. params[:file_name]
complete: function () {
},
success: function (data, textStatus, xhr) {
console.log('Got the tweets... Drawing marker.');
var marker = [];
for (var i = 0; i < data.length; i++) {
if (!($.isEmptyObject(data[i].geo))) {
var computed_latitude = data[i].geo.coordinates[0];
var computed_longitude = data[i].geo.coordinates[1];
myLatlng = {lat: computed_latitude, lng: computed_longitude};
marker[i] = new google.maps.Marker({
position: myLatlng,
map: map,
title: data[i].text
});
var id = data[i].id_str;
var name = data[i].user.screen_name;
function some_function(name, id) {
window.location = 'https://twitter.com/'+ name + '/status/' + id ;
};
marker[i].addListener("click", function(){
some_function(name, id);
}, false);
}
}
},
error: function () {
alert("Ajax error!")
}
});
}, function () {
handleLocationError(true, infoWindow, map.getCenter());
});
} else {
// Browser doesn't support Geolocation
handleLocationError(false, infoWindow, map.getCenter());
}
}
function handleLocationError(browserHasGeolocation, infoWindow, pos) {
infoWindow.setPosition(pos);
infoWindow.setContent(browserHasGeolocation ?
'Error: The Geolocation service failed.' :
'Error: Your browser doesn\'t support geolocation.');
}
在我的案例中它做了什么:
获取用户位置,然后使用Long
Gem将Lat
,api/map_controller.rb
发送到Twitter
。它搜索了半径10公里范围内的附近公共推文。没有查询字符串。控制器以JSON格式响应基于javascript的谷歌地图。然后它附加推文来映射。