http://developer.android.com/reference/android/location/Location.html
Посмотрите на distanceTo или distanceBetween. Вы можете создать объект Location из широты и долготы:
Location locationA = new Location("point A");
locationA.setLatitude(latA);
locationA.setLongitude(lngA);
Location locationB = new Location("point B");
locationB.setLatitude(latB);
locationB.setLongitude(lngB);
float distance = locationA.distanceTo(locationB);
или
private double meterDistanceBetweenPoints(float lat_a, float lng_a, float lat_b, float lng_b) {
float pk = (float) (180.f/Math.PI);
float a1 = lat_a / pk;
float a2 = lng_a / pk;
float b1 = lat_b / pk;
float b2 = lng_b / pk;
double t1 = Math.cos(a1) * Math.cos(a2) * Math.cos(b1) * Math.cos(b2);
double t2 = Math.cos(a1) * Math.sin(a2) * Math.cos(b1) * Math.sin(b2);
double t3 = Math.sin(a1) * Math.sin(b1);
double tt = Math.acos(t1 + t2 + t3);
return 6366000 * tt;
}
Проблема в том, что вы закрываете свою функцию точкой с запятой.
Вместо этого поставьте открывающую скобку
function myFunction(){
var text_input=document.getElementById("input001").value;
if (text_input=="4"){
document.getElementById("message001").innerText="Correct!";
}
else{
document.getElementById("message001").innerText="nope";
}
}
function myFunction(){//changed line no need to add ; here
var text_input = document.getElementById("input001").value;
if (text_input == "4") {
document.getElementById("message001").innerText = "Correct!";
} else {
document.getElementById("message001").innerText = "nope";
}
}
Помимо того, что вы объявили только функцию с ошибкой (обратите внимание на точку с запятой сразу после имени функции), ваш код не работает, потому что вы не передаете функцию в атрибуте кнопки onclick
- вы просто передавая возвращаемое значение этой функции там. Вместо этого у вас должно быть <button onclick="myFunction">Check</button>
.
Ниже правильный код:
function myFunction() {
var text_input = document.getElementById("input001").value;
if (text_input == "4") {
document.getElementById("message001").innerText = "Correct!";
} else {
document.getElementById("message001").innerText = "nope";
}
}
<div class=questions>
<p>What is 9-5?</p>
</div>
<input id="input001" type="text" />
<button onclick="myFunction">Check</button>
<p id="message001"></p>
function myFunction() {
var text_input = document.getElementById("input001").value;
if (text_input == "4") {
document.getElementById("message001").innerText = "Correct!";
} else {
document.getElementById("message001").innerText = "nope";
}
}
<div class=questions>
<p>What is 9-5?</p>
</div>
<input id="input001" type="text" />
<button onclick="myFunction()">Check</button>
<p id="message001"></p>
У вас есть синтаксические ошибки. Найдите этот пример, он будет работать.
У вас неправильный формат для определения function
, который вам нужен {
после создания функции для области
function myFunction(){
// your code
}
function myFunction(){
var text_input=document.getElementById("input001").value;
if (text_input=="4"){ document.getElementById("message001").innerText="Correct!"
}
else{
document.getElementById("message001").innerText="nope"
}
}
<!--first question-->
<div class=questions>
<p>What is 9-5?</p>
</div>
<input id="input001" type="text" />
<button onclick="myFunction()">Check</button>
<p id="message001"></p>