I want to pass longitude and latitude to the geonames api. Currently I have two select fields where a user selects the city longitude and the corresponding city latitude:
<select id="selectLat">
<option value="30.26057">Chicago Latitude</option>
<option value="51.5074">London Latitude</option>
<option value="50.0755">Prague Latitude</option>
</select>
<select id="selectLng">
<option value="-97.771889">Chicago Longitude</option>
<option value="0.1278">London Longitude</option>
<option value="14.4378">Prague Longitude</option>
</select>
Once selections are submitted the values are passed to the geonames api and I'm able use the JSON data returned. But what I actually want to happened is that a user simply selects the city name only. The longitude and latitude values would be combined and are then somehow parsed/separated and passed to the api. Are there ways to do this?
I'm using jQuery and php to handle the api request btw.
$.ajax({
url: "test.php",
type: 'POST',
dataType: 'json',
data: {
lat: $('#selectLat').val(),
lng: $('#selectLng').val()
},
You could use data-
attributes, eg:
<option data-longitude="..." data-latitude="...">city</option>
then
$.ajax({
url: "test.php",
type: 'POST',
dataType: 'json',
data: {
lat: $('#citySelect option:selected').data("latitude"),
lng: $('#citySelect option:selected').data("longitude"),
},
Maintain an array so that you have for each city its longitude and latitude, When selecting a city you will remove the information from the array from the array.
HTML:
<select id="select_city">
<option value="chicago">Chicago Latitude</option>
<option value="london">London Latitude</option>
<option value="prague">Prague Latitude</option>
</select>
JS:
const c = "{'prague' : {'latitude': 50.0755, 'longitude': 14.4378}, 'london': {'latitude': 50.0755, 'longitude':14.4378}}";
.
.
.
lat = c[${'#select_city').val()].latitude;
lng = c[${'#select_city').val()].longitude;
$.ajax({
url: "test.php",
type: 'POST',
dataType: 'json',
data: {
lat: lat,
lng: lng
},
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.