【问题标题】:Blackberry - Beginning GPS coding, app won't find location黑莓 - 开始 GPS 编码,应用程序找不到位置
【发布时间】:2011-09-04 00:34:44
【问题描述】:

我正在学习使用 Blackberry 的 GPS,并且一直在关注 Blackberry 入门书籍中有关 GPS 的部分,但代码中的某些内容是错误的,因为该应用程序只能到达“获取位置:”。我尝试从繁荣中下载源代码,以防我错过了一些东西,但这也不起作用。我知道我的设备的 GPS 工作正常,因为我已经测试了 SDK 中的示例应用程序。下面是我的代码。

LocationHandler.java

public class LocationHandler extends Thread{
    private MyScreen screen;

    public LocationHandler(MyScreen screen){
        this.screen = screen;
    }

    public void  run(){
        Criteria criteria = new Criteria();
        criteria.setVerticalAccuracy(50);
        criteria.setHorizontalAccuracy(50);
        criteria.setCostAllowed(true);
        criteria.setPreferredPowerConsumption(Criteria.POWER_USAGE_HIGH);

        try{
            screen.setMessage("Getting location...");
            LocationProvider provider = LocationProvider.getInstance(criteria);
            Location location = provider.getLocation(-1);

            QualifiedCoordinates qualifiedCoordinates = location.getQualifiedCoordinates();

            screen.setLocation(qualifiedCoordinates.getLongitude(), qualifiedCoordinates.getLatitude());
            String message = "Successfully got location, method: ";

            int method = location.getLocationMethod();

            if((method & Location.MTA_UNASSISTED)==Location.MTA_ASSISTED){
                message += "Unassisted GPS";
            }

            if((method & Location.MTE_CELLID)==Location.MTE_CELLID){
                message += "Cell site";
            }

            message += "\nHorizontal (Longitude) Accuracy: ";
            message += qualifiedCoordinates.getHorizontalAccuracy();

            message += "\nVertical (latitude) Accuracy: ";
            message += qualifiedCoordinates.getVerticalAccuracy();

            screen.setMessage(message);
        }catch(LocationException e){
            screen.setMessage("Location Exception: " + e.getMessage());
        }catch (InterruptedException ex){
            screen.setMessage("InteruptedException: " + ex.getMessage());
        }
    }
}

MyScreen.java

public final class MyScreen extends MainScreen
{
    private LabelField latitudeLbl;
    private LabelField longitudeLbl;
    private RichTextField messageField;

    public MyScreen()
    {        
    HorizontalFieldManager latManager = new HorizontalFieldManager();
    latManager.add(new LabelField("Latitude: "));
    latitudeLbl = new LabelField("");
    latManager.add(latitudeLbl);

    add(latManager);

    HorizontalFieldManager longManager = new HorizontalFieldManager();
    longManager.add(new LabelField("Longitude: "));
    longitudeLbl = new LabelField("");
    longManager.add(longitudeLbl);

    add(longManager);
    messageField = new RichTextField();
    add(messageField);
    }

    private void update(){
        LocationHandler handler = new LocationHandler(this);
        handler.start();
    }

    protected void makeMenu(Menu menu, int instance) {
        super.makeMenu(menu, instance);
        menu.add(new MenuItem("Update", 10, 10) {
        public void run() {
        update();
        }
        });
    }

    public void setLocation(double longitude, double latitude){
        synchronized(UiApplication.getEventLock()){
            longitudeLbl.setText(Double.toString(longitude));
            latitudeLbl.setText(Double.toString(latitude));
        }
    }

    public void setMessage(String message){
        synchronized(UiApplication.getEventLock()){
            messageField.setText(message);
        }
    }
}

【问题讨论】:

    标签: java blackberry gps


    【解决方案1】:

    尝试检测您是否在LocationProvider.getInstance(criteria) 获得LocationException。如果是,则操作系统无法找到与您的条件 LP 匹配的内容,因此请尝试使用其他条件。

    如果它确实返回了一个有效的 LP,那么它仍然可能在 provider.getLocation(-1) 上失败。有各种例外/情况。详情请查看API。在大多数情况下,您会收到LocationException(如果无法检索位置或超时期限已过)或SecurityException(如果调用应用程序无权查询位置信息)。

    【讨论】:

    • 顺便说一句,BB GPS API 简直糟透了。使用他们的 API 创建一个漂亮的 GPS 应用程序是非常困难的。如果您为 OS 5 或 6 进行开发,那么您可以从更新的 BB GPS API(JSR 179 的 BlackBerry 扩展)中获益。检查 net.rim.device.api.gps.BlackBerryLocationProvider 类。
    • 感谢 Arhimed 的回复。我将如何检查该行的 LocationException ?顺便说一句,我在设备上调试,但是当我尝试在模拟器上调试时,它工作了。
    • 通过用 try-catch 块换行。模拟器在模拟 GPS 方面做得不好:(1)它忽略了权限模型(好像所有应用程序权限都设置为“允许”),(2)它在真实设备上立即提供位置更新,这可能是一个耗时的操作.
    • 再次感谢。我尝试包装 getLocation() 和 getInstance 并且都没有返回任何异常,所以我将听从您的建议并研究较新的 api。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多