2012-09-03 37 views
0

我有2个文本框,我在其中提供源地址和目标地址。从Android中的地理编码器获得错误的距离

1)源= SHAHIBAUG桥下,Shahibagh,Ahmedab​​ad,古吉拉特

2)目的地= CG路Shreyas菌落,Navrangpura,Ahmedab​​ad,古吉拉特

距离= 4.6(从谷歌地图) & & 2.6656852(使用位置的distanceTo法)

final ProgressDialog dialog = ProgressDialog.show(AutoActivity.this, "Fare", "test"); 

      new Thread(new Runnable() { 
       @Override 
       public void run() { 
        try { 
         Location source_location = new Location(""); 
         Location destination_location = new Location(""); 
         Geocoder geoCode = new Geocoder(getApplicationContext()); 

         source_location.setLatitude(geoCode.getFromLocationName(source_input.getText().toString(), 1).get(0).getLatitude()); 
         source_location.setLatitude(geoCode.getFromLocationName(source_input.getText().toString(), 1).get(0).getLongitude()); 
         source_location.set(source_location); 

         destination_location.setLatitude(geoCode.getFromLocationName(destination_input.getText().toString(), 1).get(0).getLatitude()); 
         destination_location.setLatitude(geoCode.getFromLocationName(destination_input.getText().toString(), 1).get(0).getLongitude()); 
         destination_location.set(destination_location); 

         float distance = source_location.distanceTo(destination_location)/1000; 
         System.out.println("Distance == " + distance); 
         kmVal = BigDecimal.valueOf(distance); 
         calculateFares(); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } catch (Exception e) { 
         e.printStackTrace(); 
        } finally { 
         if (dialog!=null) { 
          dialog.dismiss(); 
         } 
        } 
       } 
      }).start(); 

我不知道为什么我错了距离。我在我的代码中有一个疑问,那就是我使用了getFromLocationName方法,并且在传递1个参数来获得一个结果。这有什么区别吗?任何人有任何想法,请帮助。

+0

尝试计算来源lat,lng和目标lat的距离 –

+0

我需要这2个位置的道路距离。是否可以通过Android中提供的地理编码器和位置API或必须使用Google地图。 – Scorpion

+0

检查此问题http://stackoverflow.com/questions/7696112/gettting-different-distance-when-using-the-google-map-and-user-defined-function/7752341#7752341 – MKJParekh

回答

1

使用Google Maps API解决了这个问题。希望它能帮助其他人。它给你准确的距离。

final ProgressDialog dialog = ProgressDialog.show(AutoActivity.this, "", "Calculating..."); 

      new Thread(new Runnable() { 
       @Override 
       public void run() { 
        try { 
         float distance = 0.0f; 
         StringBuffer url = new StringBuffer("http://maps.googleapis.com/maps/api/directions/json?origin="); 
         url.append(source_input.getText().toString().replace(" ", "").trim().toString()); 
         url.append("&destination="); 
         url.append(destination_input.getText().toString().replace(" ", "").trim().toString()); 
         url.append("&sensor=false"); 

         DefaultHttpClient httpClient = new DefaultHttpClient(); 
         HttpGet httpGet = new HttpGet(url.toString()); 
         HttpResponse httpResponse = httpClient.execute(httpGet); 
         HttpEntity httpEntity = httpResponse.getEntity(); 
         String line = EntityUtils.toString(httpEntity); 

         JSONObject jsonObject = new JSONObject(line); 
         JSONArray jsonarray = jsonObject.getJSONArray("routes").getJSONObject(0).getJSONArray("legs"); 

         for (int i = 0; i < jsonarray.length(); i++) { 
          JSONObject obj = jsonarray.getJSONObject(i); 
          distance = Float.valueOf(obj.getJSONObject("distance").getString("text").replace("km", "")); 
          System.out.println("Distance == " + obj.getJSONObject("distance").getString("text").replace("km", "")); 
         } 


         kmVal = BigDecimal.valueOf(distance); 
         calculateFares(); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } catch (Exception e) { 
         e.printStackTrace(); 
        } finally { 
         if (dialog!=null) { 
          dialog.dismiss(); 
         } 
        } 
       } 
      }).start(); 
1

谷歌地图给你的道路距离,地理编码器给你的距离与乌鸦苍蝇。

+0

嗯..右..所以有没有解决方案。 – Scorpion

+0

解决方案是什么?这些是不同的事情。你想获得什么? – njzk2

+0

我想要道路距离... – Scorpion