Javascript gotLocationJSON - jcobban/Genealogy GitHub Wiki

function gotLocationJSON(response)

Up: Location Services

The function gotLocationJSON handles the Javascript Object Notation (JSON) response from invocation of the script /Family/getLocationJSON.php to a search of the database for matching instances of class Location requested by function locationChanged.

If there is only one Location which starts with the string value then the full name of the Location is supplied. If there is more than one Location which starts with the string value then a dialog is displayed to permit selecting one existing Location. If there is no matching Location the application displays a warning that a new Location will be created when the records is updated. For example if you enter "London" this matches the pre-defined Location "London, ON, CA".

It modifies the input field or displays a dialog based upon the response.

The response from the server is a JSON document such as:

{
    "parms" : {
        "name" : "indiana"
    },
    "count" : "1",
    "cmd" : "SELECT * FROM tblLR WHERE LEFT(`location`, 7)='indiana' ORDER BY SortedLocation, IDLR LIMIT 40",
    "locations" : { 
        "1" :
       {
	"idlr":	33475,
	"fsplaceid":	"",
	"preposition":	"",
	"location":	"Indiana, USA",
	"sortedlocation":	"Indiana, USA",
	"shortname":	"Indiana, USA",
	"tag1":	        0,
	"used":	        0,
	"notes":	"",
	"verified":	0,
	"latitude":	"0.0000000000",
	"longitude":	"0.0000000000",
	"fsresolved":	0,
	"veresolved":	0,
	"qstag":	0,
	"zoom":	        14,
	"boundary":	""
        }
    }
}

The member response.parms.name is the string that was entered by the user into an <input type="text"> that has locationChanged as its change event handler. Since the response has a single match the value of that input field is replaced by the content of the location member of the first member of response.locations. Note that if the original string started with the word "House" or the word "Residence" then the first part of the string, up to and including the first comma, is assumed to be a description of the location, not part of the address, and so it is separated from the address for the search and then pre-pended to the response. So for example entering "House of John Smith, Wellington St, London" will expand the entered value to "House of John Smith, Wellington St, London, ON, CA". This simplifies data entry and avoids a pointless warning to the user.

{
    "parms" : {
        "name" : "indi"
    },
    "count" : "5",
    "cmd" : "SELECT * FROM tblLR WHERE LEFT(`location`, 4)='indi' ORDER BY SortedLocation, IDLR LIMIT 40",
    "locations" : {
    "1" :
       {
	"idlr":	18896,
	"fsplaceid":	"",
	"preposition":	"",
	"location":	"India?",
	"sortedlocation":	"India?",
	"shortname":	"India?",
        ...
       }
    "2" :
       {
	"idlr":	33475,
	"fsplaceid":	"",
	"preposition":	"",
	"location":	"Indiana, USA",
	"sortedlocation":	"Indiana, USA",
	"shortname":	"Indiana, USA",
	...
        }
        ...
    }
}

This response has multiple matches so the method presents the matches in alphabetical order and prompts the user to choose one.

Prompt to Choose from Mutliple Location Matches

Next: function locationChosen(event)