0

I'm trying to populate LatLng's with coords from jsp but i'm newbie in js. Could You give me some hints how do this:)?

i have in my jsp google maps api v3 that show map and draw simple path

<script type="text/javascript">

function initialize() {
  var myLatLng = new google.maps.LatLng(0, -180);
  var myOptions = {
    zoom: 3,
    center: myLatLng,
    mapTypeId: google.maps.MapTypeId.TERRAIN
  };

  var map = new google.maps.Map(document.getElementById("map_canvas"),
      myOptions);

  var flightPlanCoordinates = [
                                new google.maps.LatLng(37.772323, -122.214897),
                                new google.maps.LatLng(21.291982, -157.821856),
                                new google.maps.LatLng(-18.142599, 178.431),
                                new google.maps.LatLng(-27.46758, 153.027892)
                              ];

  var flightPath = new google.maps.Polyline({
    path: flightPlanCoordinates,
    strokeColor: "#FF0000",
    strokeOpacity: 1.0,
    strokeWeight: 2
  });

  flightPath.setMap(map);
}

function loadScript() {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "http://maps.googleapis.com/maps/api/js?xxx&sensor=false&callback=initialize";
document.body.appendChild(script);
}

window.onload = loadScript;

</script>

below im getting coords from my controller

<c:forEach items="${trackpoints}" var="coord">
<div>lat = ${coord.latitude}, lon = ${coord.longitude}</div>
</c:forEach>

now i want to replace hardcoded flightPlanCoordinates in google maps api with my coords from "trackpoints"

i spend few hours but with no luck;/

1 Answer 1

2

Have you tried just printing them out in the array constructor?

var flightPlanCoordinates = [<c:forEach items="${trackpoints}" var="coord">
    new google.maps.LatLng(${coord.latitude}, ${coord.longitude}),
    </c:forEach>];

Remember: the JavaScript isn't executed until its in the browser, so you can dynamically generate it. you'll need to figure out how to omit the last , though as that will mess up IE

Sign up to request clarification or add additional context in comments.

1 Comment

Excellent. If it's working, you should accept the answer to show that this solution is correct. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.