无法将字符串解析为 GeoCoordinate HERE 地图。我的方法 resolveAddress 不起作用
not able to resolve string to GeoCoordinate HERE maps. My method resolveAddress is not working
//我的变量
private static GeoCoordinate fromLocation;
私有静态 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);
当我输入 fromLocation 和 toLocation 时,我按下 Go! UI 中的按钮并调用 getDirections 方法。在此我试图将用户以文本字符串形式输入的地址解析为地理坐标。
// 点击 "Go!" 按钮的功能
public无效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();
}
}
这是returns字符串查询地理坐标的方法。但这是行不通的。
public地理坐标解析地址(字符串查询)
{
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");
}
}
}
}
- gson jar 是否如文档中所述添加到 app 的 libs 文件夹中 (https://developer.here.com/mobile-sdks/documentation/android-hybrid-plus/topics/app-simple.html)
- 在进行地址解析请求时设置搜索中心,示例如下:
GeocodeRequest 请求 = new GeocodeRequest(query).setSearchArea(mMap.getCenter(),5000);
- 请仅在 GeocodeListner 的 onCompleted() 方法中解析结果,而不是 resolveAddress()
//我的变量
private static GeoCoordinate fromLocation; 私有静态 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);
当我输入 fromLocation 和 toLocation 时,我按下 Go! UI 中的按钮并调用 getDirections 方法。在此我试图将用户以文本字符串形式输入的地址解析为地理坐标。
// 点击 "Go!" 按钮的功能 public无效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();
}
}
这是returns字符串查询地理坐标的方法。但这是行不通的。 public地理坐标解析地址(字符串查询) {
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");
}
}
}
}
- gson jar 是否如文档中所述添加到 app 的 libs 文件夹中 (https://developer.here.com/mobile-sdks/documentation/android-hybrid-plus/topics/app-simple.html)
- 在进行地址解析请求时设置搜索中心,示例如下: GeocodeRequest 请求 = new GeocodeRequest(query).setSearchArea(mMap.getCenter(),5000);
- 请仅在 GeocodeListner 的 onCompleted() 方法中解析结果,而不是 resolveAddress()