کلیدستان

نسخه‌ی کامل: نشان دادن فاصله دو نقطه با GPS
شما در حال مشاهده نسخه آرشیو هستید. برای مشاهده نسخه کامل کلیک کنید.
سلام دوستان عزیز
میخوام برنامه ای بنویسم که دو نقطه جغرافیایی را بهش بدیم و فاصله این دو نقطه رو به ما نشون بده(با استفاده از GPS)

لطفا کمک کنییییییییید.مرسی
میتونید از کد زیر استفاده کنید

کد پی‌اچ‌پی:
Location loc1 = new Location("");
loc1.setLatitude(lat1);
loc1.setLongitude(lon1);

Location loc2 = new Location("");
loc2.setLatitude(lat2);
loc2.setLongitude(lon2);

float distanceInMeters loc1.distanceTo(loc2); 

مرجع : 
http://developer.android.com/reference/a....Location)

البته توصیه شده بجای استفاده از تابع distanceTo از تابع distanceBetween استفاده کنید

Location.distanceBetween()
(۱۳۹۵/۰۲/۲۲, ۰۸:۱۶ ق.ظ)mahdi10539 نوشته: [ -> ]میتونید از کد زیر استفاده کنید

کد پی‌اچ‌پی:
Location loc1 = new Location("");
loc1.setLatitude(lat1);
loc1.setLongitude(lon1);

Location loc2 = new Location("");
loc2.setLatitude(lat2);
loc2.setLongitude(lon2);

float distanceInMeters loc1.distanceTo(loc2); 

مرجع : 
http://developer.android.com/reference/a....Location)

البته توصیه شده بجای استفاده از تابع distanceTo از تابع distanceBetween استفاده کنید

Location.distanceBetween()

مهدی جان من میخوام که با استفاده از map  باشه
این یکی از دقیقترین متدهایی هستش که برای محاسبه فاصله دو نقطه از روی نقشه استفاده میشه
Google map v2

فقط کافیه مختصات دو نقطه رو از روی نقشه بدست بیارید و بهش بدید فاصله رو محاسبه میکنه

کد پی‌اچ‌پی:
erfect.

public 
double CalculationByDistance(LatLng StartPLatLng EndP) {
 
       int Radius=6371;//radius of earth in Km         
 
       double lat1 StartP.latitude;
 
       double lat2 EndP.latitude;
 
       double lon1 StartP.longitude;
 
       double lon2 EndP.longitude;
 
       double dLat Math.toRadians(lat2-lat1);
 
       double dLon Math.toRadians(lon2-lon1);
 
       double a Math.sin(dLat/2) * Math.sin(dLat/2) +
 
       Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
 
       Math.sin(dLon/2) * Math.sin(dLon/2);
 
       double c Math.asin(Math.sqrt(a));
 
       double valueResultRadius*c;
 
       double km=valueResult/1;
 
       DecimalFormat newFormat = new DecimalFormat("####");
 
       int kmInDec  Integer.valueOf(newFormat.format(km));
 
       double meter=valueResult%1000;
 
       int  meterInDecInteger.valueOf(newFormat.format(meter));
 
       Log.i("Radius Value",""+valueResult+"   KM  "+kmInDec+" Meter   "+meterInDec);

 
       return Radius c;
 
    
این متد هم فاصله رو بر مبنای جاده و مسیر زمینی محاسبه میکنه. حالا چقد دقیق و بهینه باشه رو نمیدونم

کد پی‌اچ‌پی:
private String getDistanceOnRoad(double latitudedouble longitude,
 
           double prelatitutedouble prelongitude) {
 
       String result_in_kms "";
 
       String url "http://maps.google.com/maps/api/directions/xml?origin="
 
               latitude "," longitude "&destination=" prelatitute
                
"," prelongitude "&sensor=false&units=metric";
 
       String tag[] = { "text" };
 
       HttpResponse response null;
 
       try {
 
           HttpClient httpClient = new DefaultHttpClient();
 
           HttpContext localContext = new BasicHttpContext();
 
           HttpPost httpPost = new HttpPost(url);
 
           response httpClient.execute(httpPostlocalContext);
 
           InputStream is response.getEntity().getContent();
 
           DocumentBuilder builder DocumentBuilderFactory.newInstance()
 
                   .newDocumentBuilder();
 
           Document doc builder.parse(is);
 
           if (doc != null) {
 
               NodeList nl;
 
               ArrayList args = new ArrayList();
 
               for (String s tag) {
 
                   nl doc.getElementsByTagName(s);
 
                   if (nl.getLength() > 0) {
 
                       Node node nl.item(nl.getLength() - 1);
 
                       args.add(node.getTextContent());
 
                   } else {
 
                       args.add(" - ");
 
                   }
 
               }
 
               result_in_kms String.format("%s"args.get(0));
 
           }
 
       } catch (Exception e) {
 
           e.printStackTrace();
 
       }
 
       return result_in_kms;
 
   
(۱۳۹۵/۰۲/۲۳, ۰۱:۰۱ ق.ظ)mahdi10539 نوشته: [ -> ]این متد هم فاصله رو بر مبنای جاده و مسیر زمینی محاسبه میکنه. حالا چقد دقیق و بهینه باشه رو نمیدونم

کد پی‌اچ‌پی:
private String getDistanceOnRoad(double latitudedouble longitude,
 
           double prelatitutedouble prelongitude) {
 
       String result_in_kms "";
 
       String url "http://maps.google.com/maps/api/directions/xml?origin="
 
               latitude "," longitude "&destination=" prelatitute
                
"," prelongitude "&sensor=false&units=metric";
 
       String tag[] = { "text" };
 
       HttpResponse response null;
 
       try {
 
           HttpClient httpClient = new DefaultHttpClient();
 
           HttpContext localContext = new BasicHttpContext();
 
           HttpPost httpPost = new HttpPost(url);
 
           response httpClient.execute(httpPostlocalContext);
 
           InputStream is response.getEntity().getContent();
 
           DocumentBuilder builder DocumentBuilderFactory.newInstance()
 
                   .newDocumentBuilder();
 
           Document doc builder.parse(is);
 
           if (doc != null) {
 
               NodeList nl;
 
               ArrayList args = new ArrayList();
 
               for (String s tag) {
 
                   nl doc.getElementsByTagName(s);
 
                   if (nl.getLength() > 0) {
 
                       Node node nl.item(nl.getLength() - 1);
 
                       args.add(node.getTextContent());
 
                   } else {
 
                       args.add(" - ");
 
                   }
 
               }
 
               result_in_kms String.format("%s"args.get(0));
 
           }
 
       } catch (Exception e) {
 
           e.printStackTrace();
 
       }
 
       return result_in_kms;
 
   

یه دنیا ممنونتم 
قربوووووووووونت