Hi @Dotnet Engineer, I changed your code based on the requirement in your question, I hope it is what you want.
Home/Index
$.ajax({
url: "/Home/ReverseGeocode",
type: "POST",
contentType: "application/json; charset=utf-8",
data: JSON.stringify(data),
success: function (response) {
console.log(response);
$('#latitude').text(response.latitude)
$('#longitude').text(response.longitude)
$('#address').text(response.address)
$('#weather').text(response.weather)
$('#temperature').text(response.temperature + " °C")
}
})
//..........other code..............
<body>
<button onclick="getLocation()">GetLocation</button> <br>
Latitude : <h4 id="latitude"> @ViewData["latitude"]</h4>
Longitude: <h4 id="longitude"> @ViewData["longitude"]</h4>
Address : <h4 id="address"> @ViewData["address"]</h4>
weather : <h4 id="weather"> @ViewData["weather"]</h4>
temperature :
<br />
<label><input type="radio" name="temperature" value="C" checked />Celsius</label>
<label><input type="radio" name="temperature" value="F" />Fahrenheit</label>
<h4 id="temperature"> </h4>
<br>
<h4> Latitude and Longtitude from browser , address from Bing Maps
</h4>
<script>
const temperatureElement = document.getElementById("temperature");
document.querySelectorAll('input[name="temperature"]').forEach((radio) => {
radio.addEventListener("change", () => {
updateTemperature(temperatureElement.textContent, radio.value);
});
});
function updateTemperature(value, unit) {
const newValue = convertTemperature(value, unit);
temperatureElement.textContent = newValue;
}
function convertTemperature(value, unit) {
const numericValue = parseFloat(value);
if (isNaN(numericValue)) {
return "Invalid input";
}
if (unit === "C") {
return ((numericValue - 32) * (5 / 9)).toFixed(1) + " °C";
} else if (unit === "F") {
return ((numericValue * 9 / 5) + 32).toFixed(1) + " °F";
} else {
return "Invalid unit";
}
}
</script>
</body>
//..........other code..............
Gif Demo
Distance/index
@if (ViewData["CalculatedDistance"] != null)
{
<label class="control-label">choose your unit</label>
<br />
<select id="distance" onchange="change()">
<option value="K">Kilometers</option>
<option value="M">Miles</option>
</select>
<div >
Distance = <div id="unit">@ViewData["CalculatedDistance"] km</div>
</div>
}
//.......other code
@section Scripts{
<script>
function change(){
const value = document.getElementById("distance").value
const result = parseFloat(document.getElementById("unit").textContent);
if (value === 'K'){
const km = (result * 1.609344).toFixed(1)
document.getElementById("unit").innerHTML = km + ' km'
}else if(value === 'M'){
const m = (result * 0.6213711922).toFixed(1)
document.getElementById("unit").innerHTML = m + ' miles'
}
}
</script>
}
Gif Demo
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Xinran Shen