2016-07-22 64 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(); 
    } 

這應該可以解決您的問題。