Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8995915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:40:59+00:00 2026-06-15T23:40:59+00:00

Iam having some problems with a function I wrote. I want to wrap this

  • 0

Iam having some problems with a function I wrote.
I want to wrap this whole thing into a function called myClosestCity that returns the name of the city based on a JSON feed that gives coordinates from users IP. As far as I can see the problem is within the getJSON function.
I’ve tried with global variables, getters and setters (which didnt work at all) and just about everything I could think of Googling.

By the way, for the code to run you’ll need to include a script from Google Maps:
http://maps.google.com/maps/api/js?sensor=false&libraries=geometry

Anyway, here’s my whole code:

var stavanger = new google.maps.LatLng(58.96998, 5.73311);
var oslo = new google.maps.LatLng(59.91387, 10.75225);
var trondheim = new google.maps.LatLng(63.43051, 10.39505);
var bergen = new google.maps.LatLng(60.39126, 5.32205);

function calcDistance(p1, p2){
  return (google.maps.geometry.spherical.computeDistanceBetween(p1, p2) / 1000).toFixed(2);
}



$.getJSON("http://www.geoplugin.net/json.gp?jsoncallback=?", function(data) {
myLocation = new google.maps.LatLng(data.geoplugin_latitude,data.geoplugin_longitude);
var distances = new Array();
distances[0] = calcDistance(myLocation, stavanger);
distances[1] = calcDistance(myLocation, oslo);
distances[2] = calcDistance(myLocation, trondheim);
distances[3] = calcDistance(myLocation, bergen);



maxValue = Math.min.apply(this, distances);
var findThisIndex = maxValue + "";
var placeNo = $.inArray(findThisIndex, distances);


if(placeNo == 0) {
    closestCity = "Stavanger";
}

else if (placeNo == 1){
    closestCity = "Oslo";
}

else if(placeNo == 2) {
    closestCity = "Trondheim";
}

else if(placeNo == 3){
    closestCity = "Bergen";
}

else {
    closestCity = "Ukjent"; // Unknown in Norwegian
}

alert(closestCity);
  });

Update: It’s the closestCity variable I’d like to return!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T23:41:00+00:00Added an answer on June 15, 2026 at 11:41 pm

    You can define your function and then wait for server response, then use your closestCity.
    You cand modify your code in this way:

    var myClosestCity = function(callback) {
        var stavanger = new google.maps.LatLng(58.96998, 5.73311);
        var oslo = new google.maps.LatLng(59.91387, 10.75225);
        var trondheim = new google.maps.LatLng(63.43051, 10.39505);
        var bergen = new google.maps.LatLng(60.39126, 5.32205);
    
        function calcDistance(p1, p2) {
            return (google.maps.geometry.spherical.computeDistanceBetween(p1,      p2) / 1000).toFixed(2);
        }
    
    
        $.getJSON("http://www.geoplugin.net/json.gp?jsoncallback=?", function(data) {
            myLocation = new google.maps.LatLng(data.geoplugin_latitude, data.geoplugin_longitude);
            var distances = [];
            distances[0] = calcDistance(myLocation, stavanger);
            distances[1] = calcDistance(myLocation, oslo);
            distances[2] = calcDistance(myLocation, trondheim);
            distances[3] = calcDistance(myLocation, bergen);
    
    
    
            maxValue = Math.min.apply(this, distances);
            var findThisIndex = maxValue + "";
            var placeNo = $.inArray(findThisIndex, distances);
    
    
            if (placeNo === 0) {
                closestCity = "Stavanger";
            }
    
            else if (placeNo == 1) {
                closestCity = "Oslo";
            }
    
            else if (placeNo == 2) {
                closestCity = "Trondheim";
            }
    
            else if (placeNo == 3) {
                closestCity = "Bergen";
            }
    
            else {
                closestCity = "Ukjent";
            }
    
            if (typeof callback === "function") {
                callback.call(null, closestCity);
            }
        });
    };
    
    
    //call the function and display the closestCity on callback
    myClosestCity(function(closestCity) {
        alert(closestCity);
    });?
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having some problems with the PDF files that I make using the
I am having some problems turning the SQL below into a Zend Db query.
I'm having some trouble with this code. I am trying to write a function
I am having some problems with a javascipt function which I'm working on. Here
I am having an issue with recv . I wrote a function that fills
I am having some problems with finding out if the value is missing or
I am having some problems designing a schema for dynamic properties with versioning. Assume
I am having some problems trying to add a ValueConverter in my xaml usercontrol:
I am having some problems with MySQL and selecting column count from a joined
I am having some problems writing to a file in unicode inside my c

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.