草庐IT

android - J2ME/Android/BlackBerry - 行车路线,两个地点之间的路线

coder 2023-05-07 原文

在 Android 1.0 上,有一个用于行车路线的 com.google.googlenav 命名空间:
Route - Improved Google Driving Directions
但在较新的 SDK 中,由于某种原因它被删除了...
Android: DrivingDirections removed since API 1.0 - how to do it in 1.5/1.6? 在黑莓上,也缺少此类东西的 API:
how to find the route between two places in Blackberry?

csie-tw 提供了一种解决方法(查询 gmaps 以获取 kml 文件并对其进行解析):
Android - Driving Direction (Route Path)
还有Andrea发了DrivingDirections helper classes安卓版。
我在 j2me 中为此功能编写了一个小助手,因此我想在 Android 和 BlackBerry 上分享我的示例。

更新
正如评论中所述,官方不允许Google Maps APIs Terms of Service :

Google Maps/Google Earth APIs Terms of Service
Last updated: May 27, 2009
...
10. License Restrictions. Except as expressly permitted under the Terms, or unless you have received prior written authorization from Google (or, as applicable, from the provider of particular Content), Google's licenses above are subject to your adherence to all of the restrictions below. Except as explicitly permitted in Section 7 or the Maps APIs Documentation, you must not (nor may you permit anyone else to):
...
10.9 use the Service or Content with any products, systems, or applications for or in connection with:
(a) real time navigation or route guidance, including but not limited to turn-by-turn route guidance that is synchronized to the position of a user's sensor-enabled device;

并且可能会被某些应用程序禁用(不知何故,至少在 Android 上)...来自 Geocode scraping in .NET conversation :

This is not allowed by the API terms of use. You should not scrape Google Maps to generate geocodes. We will block services that do automated queries of our servers.

Bret Taylor
Product Manager, Google Maps

如果有任何替代方案和/或建议,我们将不胜感激!
谢谢!

最佳答案

J2ME map 路线提供者

maps.google.com 有一个导航服务,可以在 KML 中为您提供路线信息。格式。

要获取 kml 文件,我们需要形成带有起始和目标位置的 url:

public static String getUrl(double fromLat, double fromLon,
                            double toLat, double toLon) {// connect to map web service
    StringBuffer urlString = new StringBuffer();
    urlString.append("http://maps.google.com/maps?f=d&hl=en");
    urlString.append("&saddr=");// from
    urlString.append(Double.toString(fromLat));
    urlString.append(",");
    urlString.append(Double.toString(fromLon));
    urlString.append("&daddr=");// to
    urlString.append(Double.toString(toLat));
    urlString.append(",");
    urlString.append(Double.toString(toLon));
    urlString.append("&ie=UTF8&0&om=0&output=kml");
    return urlString.toString();
}

接下来您需要解析 xml(用 SAXParser 实现)并填充数据结构:

public class Point {
    String mName;
    String mDescription;
    String mIconUrl;
    double mLatitude;
    double mLongitude;
}

public class Road {
    public String mName;
    public String mDescription;
    public int mColor;
    public int mWidth;
    public double[][] mRoute = new double[][] {};
    public Point[] mPoints = new Point[] {};
}

Android 和 Blackberry 以不同的方式实现网络连接,因此您必须先形成 url:

 public static String getUrl(double fromLat, double fromLon,
     double toLat, double toLon)

然后用这个url创建连接并获取InputStream。
然后通过这个 InputStream 得到解析后的数据结构:

 public static Road getRoute(InputStream is) 

完整源代码RoadProvider.java

黑莓

class MapPathScreen extends MainScreen {
    MapControl map;
    Road mRoad = new Road();
    public MapPathScreen() {
        double fromLat = 49.85, fromLon = 24.016667;
        double toLat = 50.45, toLon = 30.523333;
        String url = RoadProvider.getUrl(fromLat, fromLon, toLat, toLon);
        InputStream is = getConnection(url);
        mRoad = RoadProvider.getRoute(is);
        map = new MapControl();
        add(new LabelField(mRoad.mName));
        add(new LabelField(mRoad.mDescription));
        add(map);
    }
    protected void onUiEngineAttached(boolean attached) {
        super.onUiEngineAttached(attached);
        if (attached) {
            map.drawPath(mRoad);
        }
    }
    private InputStream getConnection(String url) {
        HttpConnection urlConnection = null;
        InputStream is = null;
        try {
            urlConnection = (HttpConnection) Connector.open(url);
            urlConnection.setRequestMethod("GET");
            is = urlConnection.openInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return is;
    }
}

J2MEMapRouteBlackBerryEx 上查看完整代码在 Google 代码上

安卓

public class MapRouteActivity extends MapActivity {
    LinearLayout linearLayout;
    MapView mapView;
    private Road mRoad;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mapView = (MapView) findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);
        new Thread() {
            @Override
            public void run() {
                double fromLat = 49.85, fromLon = 24.016667;
                double toLat = 50.45, toLon = 30.523333;
                String url = RoadProvider
                        .getUrl(fromLat, fromLon, toLat, toLon);
                InputStream is = getConnection(url);
                mRoad = RoadProvider.getRoute(is);
                mHandler.sendEmptyMessage(0);
            }
        }.start();
    }

    Handler mHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            TextView textView = (TextView) findViewById(R.id.description);
            textView.setText(mRoad.mName + " " + mRoad.mDescription);
            MapOverlay mapOverlay = new MapOverlay(mRoad, mapView);
            List<Overlay> listOfOverlays = mapView.getOverlays();
            listOfOverlays.clear();
            listOfOverlays.add(mapOverlay);
            mapView.invalidate();
        };
    };

    private InputStream getConnection(String url) {
        InputStream is = null;
        try {
            URLConnection conn = new URL(url).openConnection();
            is = conn.getInputStream();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return is;
    }
    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }
}

J2MEMapRouteAndroidEx 上查看完整代码在 Google 代码上

关于android - J2ME/Android/BlackBerry - 行车路线,两个地点之间的路线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2023669/

有关android - J2ME/Android/BlackBerry - 行车路线,两个地点之间的路线的更多相关文章

  1. 安卓apk修改(Android反编译apk) - 2

    最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路

  2. python - python的进化路线图是什么 - 2

    按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visitthehelpcenter指导。关闭11年前。在哪里可以找到python的中期或长期路线图。借此,我可以了解决策者最关心的是什么,以及他们眼中这门语言的future是什么?一段时间以来,我一直在玩弄Python和Ruby,制作我在开发中需要的中小型工具,通过比较不同但相似的语言来获得乐趣和学习。Python和Ruby的许多特性可以互换,或者易于模仿。两者都引入了一些函数式风格并且发展迅速(Py300

  3. ruby - 在公共(public)基本路线之上构建路线? - 2

    我有一个共同的基本路径;说:get/base我需要执行基本身份验证并为该路径下的所有子调用工作。说:get/base/foo和get/base/bar。查看http://www.sinatrarb.com/intro.html#Helpers建议我应该能够通过使用助手来做到这一点。我正在查看pass帮助程序并在文档中触发新路由下使用call。但是,我读到的另一个建议是使用正则表达式IE%r{/base/?:(path)?}或类似的动态路由。那么:def'/base'#dosomefunkybasicauthstuffhere#toworkwithallrequesttothiscomm

  4. ruby-on-rails - 对多条路线使用同一个 Controller ? - 2

    有没有一种方法可以编写以下路由,这样您就不必每次都指定相同的Controller?...get'jobs'=>'pages#jobs'get'contact'=>'pages#contact'get'terms'=>'pages#terms'get'privacy'=>'pages#privacy' 最佳答案 这里有几个选择:在这三个中,第一个即Usingscopeas"/"将创建与问题中定义的routes创建的完全相同的路由.1。使用范围作为“/”scope"/",controller::pagesdoget'jobs'get'c

  5. ruby-on-rails - 没有命名路线的rails3中的远程form_tag - 2

    使这个实际异步发布的正确咒语是什么?form_tag:controller=>:magic,:action=>:search,:method=>post,:remote=>truedomethod=post和remote=true只是在url的末尾被压扁,而不是真正使它成为一个ajaxpost。 最佳答案 我发现唯一的方法是将url参数包装在url_for方法中。form_tagurl_for(:action=>:create,:id=>@artist.id),:remote=>truedo但是,如果您需要传递method参数,您可

  6. Android Studio开发之使用内容组件Content获取通讯信息讲解及实战(附源码 包括添加手机联系人和发短信) - 2

    运行有问题或需要源码请点赞关注收藏后评论区留言一、利用ContentResolver读写联系人在实际开发中,普通App很少会开放数据接口给其他应用访问。内容组件能够派上用场的情况往往是App想要访问系统应用的通讯数据,比如查看联系人,短信,通话记录等等,以及对这些通讯数据及逆行增删改查。首先要给AndroidMaifest.xml中添加响应的权限配置 下面是往手机通讯录添加联系人信息的例子效果如下分成三个步骤先查出联系人的基本信息,然后查询联系人号码,再查询联系人邮箱代码 ContactAddActivity类packagecom.example.chapter07;importandroid

  7. Android 10.0 设置默认launcher后安装另外launcher后默认Launcher失效的功能修复 - 2

    1.前言 在10.0的系统rom定制化开发中,在系统中有多个launcher的时候,会在开机进入launcher的时候弹窗launcher列表,让用户选择进入哪个launcher,这样显得特别的不方便所以产品开发中,要求用RoleManager的相关api来设置默认Launcher,但是在设置完默认Launcher以后,在安装一款Launcher的时候,默认Launcher就会失效,在系统设置的默认应用中Launcher选项就为空,点击home键的时候会弹出默认Launcher列表,让选择进入哪个默认Launcher.所以需要从安装Launcher的流程来分析相关的设置。来解决问题设置默认La

  8. ruby-on-rails - “remember me” 复选框的集成测试失败 - 2

    我正在关注railstutorial.org第3版,目前正在研究第8章:登录、注销。我在代码list8.51中发现了一个问题(登录时不记得测试):assert_nilcookies['remember_token']当我执行:raketest时,返回红色并出现以下错误:FAIL["test_login_without_remembering",UsersLoginTest,1.268578948]test_login_without_remembering#UsersLoginTest(1.27s)Expected""tobenil.test/integration/users_log

  9. ruby-on-rails - rails 路线 : define root to namespace - 2

    我有2个Controller:app//controllersposts_controllers.rb/mobileposts_controllers.rb我的routes.rb看起来像这样:root:to=>"posts#index"resources:postsnamespace:mobiledoroot:to=>"posts#index"resources:postsend但是当我访问/mobile时,它无论如何都会渲染第一个Controller的索引页面,也试过这个:namespace:mobiledoroot:to=>"mobile/posts#index"resources

  10. AiBote 2022 新研发的自动化框架,支持 Android 和 Windows 系统。速度非常快 - 2

    Ai-Bot基于流行的Node.js和JavaScript语言的一款新自动化框架,支持Windows和Android自动化。1、Windowsxpath元素定位算法支持支持Windows应用、.NET、WPF、Qt、Java和Electron客户端程序和ie、edgechrome浏览器2、Android支持原生APP和H5界面,元素定位速度是appium十倍,无线远程自动化操作多台安卓设备3、基于opencv图色算法,支持找图和多点找色,1080*2340全分辨率找图50MS以内4、内置免费OCR人工智能技术,无限制获取图片文字和找字功能。5、框架协议开源,除官方node.jsSDK外,用户可

随机推荐