2016-07-22 15 views
0

我想在两个地点之间找到路线,因为我发现了esri样本服务,例如:http://route.arcgis.com/arcgis/rest/services/World/Route/NAServer/Route_World。 但是,如果我使用这项服务,我得到错误为未经授权的访问安全。 我无法使用这项服务,请告诉我,如果有任何免费服务获取arcgis地图上的路线 谢谢。如何在android中获取样本路线arcgis?

我的代码:

public void getRouteFromSource(Geometry current_location,Geometry destination_point,boolean isCurrentLocation){ 
     routeLayer = new GraphicsLayer(); 
     mMapView.addLayer(routeLayer); 

     // Initialize the RouteTask 
     try { 

     String routeTaskURL = "http://route.arcgis.com/arcgis/rest/services/World/Route/NAServer/Route_World"; 
      mRouteTask = RouteTask.createOnlineRouteTask(routeTaskURL, null); 

     } catch (Exception e1) { 
      e1.printStackTrace();      
     } 

    // Add the hidden segments layer (for highlighting route segments) 
    hiddenSegmentsLayer = new GraphicsLayer(); 
    mMapView.addLayer(hiddenSegmentsLayer);  

    QueryDirections(current_location, destination_point,isCurrentLocation); 
} 
private void QueryDirections(final Geometry sourceGeometry, final Geometry destinationGeometry,boolean isCurrentLocation) { 

    // Show that the route is calculating 

    if(isCurrentLocation==false){ 
    dialog = ProgressDialog.show(mContext, PollingStationLocatorContant.plase_wait, 
      "Calculating route...", true); 
    } 

// Log.e("mLocation", "mLocation "+sourceGeometry); 
// Log.e("POINTTT", "POINTTT"+p); 
    // Spawn the request off in a new thread to keep UI responsive 
    Thread t = new Thread() { 
     private RouteResult mResults; 

     @Override 
     public void run() { 
      try { 
       // Start building up routing parameters 

       /*Point startPoint = new Point(78.4867, 17.3850); 
       Point stopPoint = new Point(79.5941, 17.9689);*/ 

     //  Log.e("mLocation.getX()",""+ p.getX()+"---"+ p.getY()); 
     //  Log.e("mLocation.getY()",""+ mLocation.getX() +"----"+ mLocation.getY()); 

       //Point startPoint = new Point(mLocation.getX(), mLocation.getY()); 
       //Point stopPoint = new Point(p.getX(), p.getY()); 

       StopGraphic point1 = new StopGraphic(sourceGeometry); 
       StopGraphic point2 = new StopGraphic(destinationGeometry);     

       Log.e("point1", ""+point1); 
       Log.e("point2", ""+point2); 
       NAFeaturesAsFeature rfaf = new NAFeaturesAsFeature(); 
       // Convert point to EGS (decimal degrees) 
       // Create the stop points (start at our location, go 
       // to pressed location) 
       rfaf.setFeatures(new Graphic[] { point1, point2 }); 
       rfaf.setCompressedRequest(true); 

     // RouteParameters r = new RouteParameters(); 

       RouteParameters rp = mRouteTask.retrieveDefaultRouteTaskParameters();     

       //rp.setImpedanceAttributeName("Length"); 
       rp.setReturnDirections(false); 
       // Assign the first cost attribute as the impedance 

       rp.setStops(rfaf); 
       // Set the routing service output SR to our map 
       // service's SR 
       rp.setOutSpatialReference(mMapView.getSpatialReference()); 
       //rp.setImpedanceAttributeName(""); 

       // Solve the route and use the results to update UI 
       // when received 
       mResults = mRouteTask.solve(rp);      

       List<Route> routes = mResults.getRoutes(); 
       Route mRoute = routes.get(0); 

       Geometry routeGeom = mRoute.getRouteGraphic().getGeometry(); 
       Graphic symbolGraphic = new Graphic(routeGeom, new SimpleLineSymbol(Color.BLUE,5)); 
       //SimpleMarkerSymbol sls = new SimpleMarkerSymbol(Color.RED, 10,STYLE.CIRCLE); 
       PictureMarkerSymbol pls=new PictureMarkerSymbol(mContext.getResources().getDrawable(R.drawable.animation_image)); 
       mMapView.setExtent(routeGeom, 20); 
       Graphic destinatonGraphic = new Graphic(sourceGeometry, pls); 
       mGraphicsLayer.addGraphic(symbolGraphic); 
       mDestinationGraphicLayer.addGraphic(destinatonGraphic); 
       mMapView.addLayer(mGraphicsLayer); 
       mMapView.addLayer(mDestinationGraphicLayer); 

      mHandler.post(mUpdateResults); 
      } catch (Exception e) { 
       mDestinationGraphicLayer.removeAll(); 
       noRouteFound=true; 


       e.printStackTrace(); 
       mHandler.post(mUpdateResults); 

      } 
     } 
    }; 
    // Start the operation 
    t.start(); 
} 

void updateUI() { 

    if(dialog!=null && dialog.isShowing()){ 
    dialog.dismiss(); 
    if(noRouteFound){ 

     Toast.makeText(mContext, "Unable to find route.Please select with in State", Toast.LENGTH_LONG).show(); 
    } 
    }  
    } 

回答

0

忽视地理编码服务(如果没有存储的数据可以被称为免费)路由服务确实需要一个令牌。

正如documentation说:

所需的参数令牌 使用此参数指定的令牌提供有权限访问该服务的用户 的身份。访问由Esri提供的服务 可提供有关如何获取此类 访问令牌的更多信息。

你可以做的是去here并注册一个免费的开发人员帐户。您将收到一个免费令牌及其相关的免费积分,您可以使用它来查询路由API。

但是,上面链接的文档显示了所有可能情况下的响应样本(错误,路由正常,未找到路由)。

0

创建一个免费的开发人员帐户后,请按照下列步骤操作。

在你的getRouteFromSource函数中用这个代替现有的代码。

TOKEN = "The token you receive after you sign up"; 
CLIENT_ID = "The client_id you receive after you sign up"; 

try { 
     UserCredentials authenticate= new UserCredentials(); 
     authenticate.setUserAccount("your username", "your password"); 
     authenticate.setUserToken(TOKEN, CLIENT_ID); 
     mRouteTask = RouteTask 
       .createOnlineRouteTask(
         "http://route.arcgis.com/arcgis/rest/services/World/Route/NAServer/Route_World", 
         authenticate); 
    } catch (Exception e1) { 
     e1.printStackTrace(); 
    } 

这应该可以解决您的问题。