13

現在地から目的地までの距離を計算してテキストビューで表示する必要があるプロジェクトを作成する必要があります。この距離は場所が変更されると更新されることに注意してください。このようなタイプのプロジェクトを作成することは可能ですか?

[注:Googleマップを実装せずに作成する必要があります。目的地のlon、latは既知です。場所を見つけて計算するだけです]

4

4 に答える 4

17

google android dev ページのドキュメントをチェックして、位置の変更をリッスンする方法を確認してください。http://developer.android.com/guide/topics/location/obtaining-user-location.html

この関数を使用して、現在の (開始) ポイントとターゲット ポイントの間の距離を決定できます。

 /**
 * using WSG84
 * using the Metric system
 */
public static float getDistance(double startLati, double startLongi, double goalLati, double goalLongi){
    float[] resultArray = new float[99];
    Location.distanceBetween(startLati, startLongi, goalLati, goalLongi, resultArray);
    return resultArray[0];
}
于 2012-05-21T10:33:02.167 に答える
12

Location.distanceBetween2 点間の直線距離を与えます。2 つの地理的なポイント間の PATH の距離が必要な場合は、このクラスを使用してこれを行うことができます。

public class GetDistance {

public String GetRoutDistane(double startLat, double startLong, double endLat, double endLong)
{
  String Distance = "error";
  String Status = "error";
  try {
      Log.e("Distance Link : ", "http://maps.googleapis.com/maps/api/directions/json?origin="+ startLat +","+ startLong +"&destination="+ endLat +","+ endLong +"&sensor=false");
        JSONObject jsonObj = parser_Json.getJSONfromURL("http://maps.googleapis.com/maps/api/directions/json?origin="+ startLat +","+ startLong +"&destination="+ endLat +","+ endLong +"&sensor=false"); 
        Status = jsonObj.getString("status");
        if(Status.equalsIgnoreCase("OK"))
        {
        JSONArray routes = jsonObj.getJSONArray("routes"); 
         JSONObject zero = routes.getJSONObject(0);
         JSONArray legs = zero.getJSONArray("legs");
         JSONObject zero2 = legs.getJSONObject(0);
         JSONObject dist = zero2.getJSONObject("distance");
         Distance = dist.getString("text");
        }
        else
        {
            Distance = "Too Far";
        }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
return Distance;


} 

}

これにより、2 点間の距離またはパス/道路の長さが得られます。

JSON api を解析する parser_Json クラスは次のとおりです。

 public class parser_Json {

public static JSONObject getJSONfromURL(String url){

    //initialize
    InputStream is = null;
    String result = "";
    JSONObject jArray = null;

    //http post
    try{
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();

    }catch(Exception e){
        Log.e("log_tag", "Error in http connection "+e.toString());
    }

    //convert response to string
    try{
        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        result=sb.toString();
    }catch(Exception e){
        Log.e("log_tag", "Error converting result "+e.toString());
    }

    //try parse the string to a JSON object
    try{
            jArray = new JSONObject(result);
    }catch(JSONException e){
        Log.e("log_tag", "Error parsing data "+e.toString());
    }

    return jArray;
}

 public static InputStream retrieveStream(String url) {

        DefaultHttpClient client = new DefaultHttpClient(); 

        HttpGet getRequest = new HttpGet(url);

        try {

           HttpResponse getResponse = client.execute(getRequest);
           final int statusCode = getResponse.getStatusLine().getStatusCode();

           if (statusCode != HttpStatus.SC_OK) { 

              return null;
           }

           HttpEntity getResponseEntity = getResponse.getEntity();
           return getResponseEntity.getContent();

        } 
        catch (IOException e) {
           getRequest.abort();

        }

        return null;

     }

}

于 2012-05-25T06:43:54.067 に答える
4

この質問に2つの異なる回答を書き留めて、2つのジオポイント間の差を計算したので、ここにコピーして貼り付けないでください。

次に、イベントを取得するために実装する必要があるのと同じ方法で、この例を参照してください。LocationlisteneronLocationChanged

その場合、上記の関数を使用して差を計算し、それらを適切に使用します。

于 2012-05-24T12:00:05.213 に答える
3

これは、更新を受信するたびにLocationServices現在地を特定し、新しい距離で更新するのに使用するのは非常に簡単です。TextView

于 2012-05-21T10:32:19.443 に答える