简体   繁体   English

将数据从Bean传递到jsf页面上的javascript。 Google API指示

[英]Pass data from Bean to javascript on jsf page. Google api directions

I need to pass waypoints to google maps javascript directions api from ManagedBean to jsf page. 我需要将航点从ManagedBean传递到google maps javascript方向api到jsf页面。 I'm trying to make JSONArray and pass it to jsf: 我正在尝试制作JSONArray并将其传递给jsf:

private JSONArray routesJSON;

with getters and setters. 与吸气剂和二传手。

Than im creating JSON : in loop im adding Strings with LatLang of waypoints to List<String> 比我创建JSON:在循环中我将带有​​LatLang航路点的字符串添加到List<String>

routes.add("new google.maps.LatLng(" + o.getLatitude() + "," + o.getLongitude()+")");

And im getting JSON : 我正在获取JSON:

[{"location":"new google.maps.LatLng(50.495121,22.1705)"},{"location":"new google.maps.LatLng(50.57082,22.06813)"},{"location":"new google.maps.LatLng(50.570549,22.047871)"},{"location":"new google.maps.LatLng(50.521389,21.912695)"},{"location":"new google.maps.LatLng(50.495121,22.1705)"}]

from: 从:

for()
array.put(obj);
}
System.out.println(array);

in JSF function for displaying route : (it works with hardcoded data) 在JSF函数中显示路线:(它适用于硬编码数据)

function calcRoute() {

              var waypts = [];                 
              var start = new google.maps.LatLng(#{someBean.startLatitude}, #{someBean.startLongitude});            
              var end = new google.maps.LatLng(49.712112, 21.50667);
              var way = #{someBean.routesJSON};
              var request = {
              origin:start,
              destination:end,
              optimizeWaypoints: true,                        
              waypoints:way;         
              travelMode: google.maps.TravelMode.DRIVING
              };
              directionsService.route(request, function(result, status) {
                if (status == google.maps.DirectionsStatus.OK) {
                  directionsDisplay.setDirections(result);
                }
              });
            }

But when im displaying that page map doesnt appear. 但是,当即时通讯显示该页面地图不会出现。 I know it's probably because of " in JSON but i dont know how to remove it. Or may there are easiest ways to pass waypoint data from Bean to jsf page? 我知道这可能是因为JSON中的" ,但我不知道如何删除它。或者也许有最简单的方法将Bean的航点数据传递到jsf页面?

Instead of returning a JSON data packet that contains javascript statements like "new google.maps.LatLng(50.495121,22.1705)", it's more sensible to return just the coordinates. 与其返回包含诸如“ new google.maps.LatLng(50.495121,22.1705)”之类的javascript语句的JSON数据包,不如只返回坐标。

So if you had a JSON packet that looks like this in your javascript (I don't know how to pass data between your Java and Javascript): 因此,如果您的javascript中有一个看起来像这样的JSON数据包(我不知道如何在Java和Javascript之间传递数据):

var routes = [
  {"lat":"50.495121", "lng":"22.1705"},
  {"lat":"50.57082", "lng":"22.06813"},
  {"lat":"50.570549", "lng":"22.047871"},
  {"lat":"50.521389", "lng":"21.912695"},
  {"lat":"50.495121", "lng":"22.1705"}
]

You can turn this into an array like so: 您可以将其变成一个数组,如下所示:

var way = [];
for (var i = 0; i < routes.length; i++) {
  way.push(new google.maps.LatLng(parseFloat(routes[i].lat), parseFloat(routes[i].lng)));
}

NB: the use of parseFloat to convert strings like "50.495121" into floating point numbers. 注意:使用parseFloat可以将“ 50.495121”之类的字符串转换为浮点数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM