Location信息对非常多有地址进行搜索的应用来说非常重要。比方对dianping这种应用来说。我们能够通过地址来获取当前位置的一些信息。在这篇文章中,我们来介绍怎样获取Scope架构中的位置信息。这个位置信息能够对我们非常多的搜索是非常重要的。

1)创建一个简单的Scope应用


我们首先打开SDK,并选择“Unity Scope”模版:

怎么在Ubuntu Scope中获取location地址信息-LMLPHP

接下来。我们选择“Empty scope”。这样我们就创建了我们的一个最主要的scope了。

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvVWJ1bnR1VG91Y2g=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" width="300" height="200" alt="">


我们能够执行我们的Scope。

这是一个最主要的Scope。


2)增加代码获取Location信息

为了获取位置信息,我们对我们的代码进行设置。首先打开"data"目录中的.ini文件,并增加LocationDataNeeded=true。这样整个文件显示为:

[ScopeConfig]
DisplayName = Scopetest Scope
Description = This is a Scopetest scope
Art = screenshot.png
Author = Firstname Lastname
Icon = icon.png LocationDataNeeded=true [Appearance]
PageHeader.Logo = logo.png

同一时候我们打开scope.cpp文件,并改动为:


#include <unity/scopes/SearchMetadata.h> // added

....

void Query::run(sc::SearchReplyProxy const& reply) {
try {
cerr << "starting to get the location" << endl; auto metadata = search_metadata();
if (metadata.has_location()) { cerr << "it has location data" << endl; auto location = metadata.location(); if (location.has_country_code()) {
cerr << "country code: " << location.country_code() << endl;
} if ( location.has_area_code() ) {
cerr << "area code: " << location.area_code() << endl;
} if ( location.has_city() ) {
cerr << "city: " << location.city() << endl;
} if ( location.has_country_name() ) {
cerr << "" << location.country_name() << endl;
} if ( location.has_altitude()) {
cerr << "altitude: " << location.altitude() << endl;
cerr << "longitude: " << location.longitude() << endl;
cerr << "latitude: " << location.latitude() << endl;
} if ( location.has_horizontal_accuracy()) {
cerr << "horizotal accuracy: " << location.horizontal_accuracy() << endl;
} if ( location.has_region_code() ) {
cerr << "region code: " << location.region_code() << endl;
} if ( location.has_region_name() ) {
cerr << "region name: " << location.region_name() << endl;
} if ( location.has_zip_postal_code() ) {
cerr << "zip postal code: " << location.zip_postal_code() << endl;
}
} .... }

我们通过打印的方式来查看我们所收到的位置信息。在手机上执行,并同一时候在desktop上执行例如以下的命令:


watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvVWJ1bnR1VG91Y2g=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">


watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvVWJ1bnR1VG91Y2g=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" width="400" height="300" alt="">


我们能够看到我们所须要的位置信息。通过这些信息,我们能够在我们的Scope中使用。

全部的源代码能够在例如以下的地址找到:

bzr branch lp:~liu-xiao-guo/debiantrial/scope



04-19 19:29
查看更多