无法将字符串解析为GeoCoordinate HERE地图。我的方法resolveAddress不起作用

时间:2015-11-30 10:42:44

标签: android geocoding here-api

//我的变量

私有静态GeoCoordinate fromLocation; private static GeoCoordinate toLocation;

private List<Location> locationList;
private EditText m_fromLocation;
private EditText m_toLocation;

//在onCreate中我定义了这些:

    m_fromLocation = (EditText)findViewById(R.id.fromText);
    m_toLocation = (EditText)findViewById(R.id.toText);

当我从Location和toLocation进入时,我按Go!来自UI的按钮和getDirections方法被调用。在这里我试图解决用户以文本字符串的形式输入到GeoCoordinates的地址。

//&#34; Go!&#34;的点击功能按键     public void getDirections(查看视图){

    fromLocation = resolveAddress(m_fromLocation.getText().toString());
    toLocation= resolveAddress(m_toLocation.getText().toString());

    // 1. clear previous results
    //textViewResult.setText("");
    if (map != null && mapRoute != null) {
        map.removeMapObject(mapRoute);
        mapRoute = null;
    }

    // 2. Initialize RouteManager
    RouteManager routeManager = new RouteManager();

    // 3. Select routing options
    RoutePlan routePlan = new RoutePlan();

    RouteOptions routeOptions = new RouteOptions();
    routeOptions.setTransportMode(RouteOptions.TransportMode.CAR);
    routeOptions.setRouteType(RouteOptions.Type.FASTEST);
    routePlan.setRouteOptions(routeOptions);

    // 4. Select Waypoints for your routes

    // START
    routePlan.addWaypoint(fromLocation);

    // END
    routePlan.addWaypoint(toLocation);

    // 5. Retrieve Routing information via RouteManagerEventListener

    RouteManager.Error error = routeManager.calculateRoute(routePlan, routeManagerListener);
    if (error != RouteManager.Error.NONE) {
        Toast.makeText(getApplicationContext(),
                "Route calculation failed with: " + error.toString(), Toast.LENGTH_SHORT)
                .show();
    }

}

这是返回字符串查询的GeoCoordinate的方法。但这不起作用。 public GeoCoordinate resolveAddress(String query)     {

    GeoCoordinate coordinate = null;

    if(!query.isEmpty()) {
        //query text fields for geo coordinates

        ResultListener<List<Location>> listener = new GeocodeListener();
        GeocodeRequest request = new GeocodeRequest(query);
        if (request.execute(listener) != ErrorCode.NONE) {
            Log.i(LOG_TAG, "Geocode request resulted with an error");

        } else {
            if (!locationList.isEmpty()) {
                coordinate = locationList.get(0).getCoordinate();
            }
        }

        return coordinate;
    }
    else
    {
        Log.d(LOG_TAG,"address is empty, cannot be resolved");

        return coordinate;
    }
}

class GeocodeListener implements ResultListener<List<Location>> {
    // Implementation of ResultListener

    @Override
    public void onCompleted(List<Location> data, ErrorCode error) {
        if (error != ErrorCode.NONE) {
            return;

        } else {

            if (!data.isEmpty()) {

                locationList = data;

            } else {
                locationList = null;
                Log.d("GeocodeListener", "location list is empty");
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

  1. 是否将gson jar添加到app的libs文件夹中,如文档(https://developer.here.com/mobile-sdks/documentation/android-hybrid-plus/topics/app-simple.html)中所述
  2. 在执行地理编码请求时设置搜索中心,示例如下: GeocodeRequest request = new GeocodeRequest(query).setSearchArea(mMap.getCenter(),5000);
  3. 请仅在GeocodeListner的onCompleted()方法中解析结果,而不是在resolveAddress()中解析结果