zzwyb89
zzwyb89

Reputation: 470

Passing local variable as a global

I am drawing a line using Google Maps V3, and I want to be able to pass a local variable loc as global, so I can use it outside of this entire code. How can I do this?

if(this.flightdetails.route_details.length > 0)
{
    $.each(this.flightdetails.posreports, function(i, nav)
    {
        var loc = new google.maps.LatLng(nav.latitude, nav.longitude);
        // Rest of code

        path[path.length] = loc;
        focus_bounds.extend(loc);
    });
}

Upvotes: 0

Views: 70

Answers (2)

dashtinejad
dashtinejad

Reputation: 6253

Define your variable global:

var loc; // global
if(this.flightdetails.route_details.length > 0)
{
    $.each(this.flightdetails.posreports, function(i, nav)
    {
        loc = new google.maps.LatLng(nav.latitude, nav.longitude); // with no var
        // Rest of code

        path[path.length] = loc;
        focus_bounds.extend(loc);
    });
}

Upvotes: 1

Niels Keurentjes
Niels Keurentjes

Reputation: 41958

var loc;
if(this.flightdetails.route_details.length > 0)
{
    $.each(this.flightdetails.posreports, function(i, nav)
    {
        loc = new google.maps.LatLng(nav.latitude, nav.longitude);
        // Rest of code

        path[path.length] = loc;
        focus_bounds.extend(loc);
    });
}
console.log(loc);

Upvotes: 2

Related Questions