如何从回调函数获得位置(结果)

时间:2019-01-20 20:17:12

标签: javascript here-api

我无法获得两个标记之间的路线 有帮助吗?

var geocodingParams = { searchText:'street abc'};

//定义一个回调函数来处理地理编码响应:

var onResult = function(result) {

  var locations = result.Response.View[0].Result,
    position,
    marker;

  // Add a marker for each location found
  for (i = 0;  i < locations.length; i++) {
  position = {
    lat: locations[i].Location.DisplayPosition.Latitude,
    lng: locations[i].Location.DisplayPosition.Longitude
  };
  marker = new H.map.Marker(position);
  map.addObject(position);

  } 

 }

//获取地理编码服务的实例:

var geocoder = platform.getGeocodingService();

geocoder.geocode(geocodingParams, onResult, onError)

//路由

function calculateRouteFromAtoB(platform) { 

  var router = platform.getRoutingService(),
    routeRequestParams = {
      mode: 'fastest;car',
      representation: 'display',
      routeattributes : 'waypoints,summary,shape,legs',
      maneuverattributes: 'direction,action',
      language: 'de-DE',
      alternatives : '3',
      waypoint0: 'position of the marker1' ,  
      waypoint1:  'position of the marker2'

    };
  router.calculateRoute(routeRequestParams,onSuccess,onError );

}

im试图在函数calculateRouteFromAtoB()中定义waypoint0和waypoint1,任何同义的对象?

1 个答案:

答案 0 :(得分:0)

您可以在地图中获取对象,然后查看它们是否为instanceOf H.map.Marker。基本上,它们就是您在地图上创建的标记。如果除了创建的标记外没有其他对象,则可以直接在这些对象上进行操作。

length = map.getObjects()。length //提供标记数

假设您只有两个标记(分别指向waypoint0和1),则可以如下编写函数。希望这会有所帮助!

function calculateRouteFromAtoB(platform) { 
  point1 = map.screenToGeo(map.getObjects()[0].getPosition().lat, map.getObjects()[0].getPosition().lng);
  point2 = map.screenToGeo(map.getObjects()[1].getPosition().lat, map.getObjects()[1].getPosition().lng);
  var router = platform.getRoutingService(),
    routeRequestParams = {
      mode: 'fastest;car',
      representation: 'display',
      routeattributes : 'waypoints,summary,shape,legs',
      maneuverattributes: 'direction,action',
      language: 'de-DE',
      alternatives : '3',
      waypoint0: point1.lat+','+point1.lng ,  
      waypoint1:  point2.lat+','+point2.lng

    };
  router.calculateRoute(routeRequestParams,onSuccess,onError );

}