0
我想要使用Google地方API唯一的lat,lng坐标来详细说明地址的详细信息。 我尝试了附近搜索,文本搜索和雷达搜索,但它也需要半径。然后我尝试了地理编码API,但它提供了多个地点的详细信息,并且我只需要一个地方的详细信息。如何使用Google place API的lat,lng只有一个地方获取确切的地点详细信息?
那么有什么办法可以做到这一点?
我想要使用Google地方API唯一的lat,lng坐标来详细说明地址的详细信息。 我尝试了附近搜索,文本搜索和雷达搜索,但它也需要半径。然后我尝试了地理编码API,但它提供了多个地点的详细信息,并且我只需要一个地方的详细信息。如何使用Google place API的lat,lng只有一个地方获取确切的地点详细信息?
那么有什么办法可以做到这一点?
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
可以设置0公里,如果需要1个结果。并通过查询特定结果client.search('Coffe Shops', geocode: str).take(15)
方法
最后补充此Javascript谷歌地图上添加并显示标记
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
,Lat
至api/map_controller.rb
然后使用Twitter
宝石。它在10公里范围内搜索附近的公共推文。没有查询字符串。控制器以JSON格式响应基于JavaScript的谷歌地图。那么它会附加推文来映射。