2017-02-23 13 views
0

私は2つの場所の間の距離を求めるプロジェクトを作成しています.1つは現在地、もう1つはユーザー入力値です。Google Mapのボタンを使用して現在地とユーザー入力フィールドの距離を確認

ボタンのクリックで正しい距離を取得する方法を教えてください。私はボタンをクリックせずに値を得ました。ボタンクリックを実装するにはどうすればよいですか?

output with map click listener

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_maps); 
    MarkerPoints = new ArrayList<>(); 
    final AutoCompleteTextView autoCompView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView); 

    autoCompView.setAdapter(new GooglePlacesAutocompleteAdapter(this, R.layout.list_item)); 
    autoCompView.setOnItemClickListener(this); 
    b1=(Button)findViewById(R.id.search_button); 
    b1.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      String location = autoCompView.getText().toString(); 
      List<Address>addressList = null; 

      if (location != null || !location.equals("")) { 
       Geocoder geocoder = new Geocoder(MapsActivity.this); 
       try { 
        addressList = geocoder.getFromLocationName(location, 1); 

       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
       Address address = addressList.get(0); 
       LatLng dest = new LatLng(address.getLatitude(), address.getLongitude()); 
       lat1=String.valueOf(address.getLatitude()); 
       lon1 = String.valueOf(address.getLongitude()); 
       Toast.makeText(MapsActivity.this, "new Lati and new longi1 "+dest, Toast.LENGTH_LONG).show(); 


       mMap.addMarker(new MarkerOptions().position(dest).title("Marker")); 
       mMap.animateCamera(CameraUpdateFactory.newLatLng(dest)); 
       // SendDataToServer(unique_id,lat1,lon1); 
       // mMap.clear(); 
       findPath(point); 

      } 



     } 
    }); 

    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
     checkLocationPermission(); 
    } 
    // Obtain the SupportMapFragment and get notified when the map is ready to be used. 
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
      .findFragmentById(R.id.map); 
    mapFragment.getMapAsync(this); 
    sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE); 

} 

private void findPath(LatLng point) { 
    if (MarkerPoints.size() >= 2) { 
     LatLng origin = MarkerPoints.get(0); 
     LatLng dest = MarkerPoints.get(1); 

     // Getting URL to the Google Directions API 
     String url = getUrl(origin, dest); 
    // Log.d("onMapClick", url.toString()); 
     FetchUrl FetchUrl = new FetchUrl(); 

     // Start downloading json data from Google Directions API 
     FetchUrl.execute(url); 
     //move map camera 
     mMap.moveCamera(CameraUpdateFactory.newLatLng(origin)); 
     mMap.animateCamera(CameraUpdateFactory.zoomTo(16)); 
    } 

} 

private void drawMarker(LatLng point) { 
    MarkerPoints.add(point); 

    // Creating MarkerOptions 
    MarkerOptions options = new MarkerOptions(); 

    // Setting the position of the marker 
    options.position(point); 
    private String getUrl(LatLng origin, LatLng dest) { 

    // Origin of route 
    String str_origin = "origin=" + origin.latitude + "," + origin.longitude; 

    // Destination of route 
    String str_dest = "destination=" + dest.latitude + "," + dest.longitude; 


    // Sensor enabled 
    String sensor = "sensor=false"; 

    // Building the parameters to the web service 
    String parameters = str_origin + "&" + str_dest + "&" + sensor; 

    // Output format 
    String output = "json"; 

    // Building the url to the web service 
    String url = "https://maps.googleapis.com/maps/api/directions/" + output + "?" + parameters; 


    return url; 
} 

/** 
* A method to download json data from url 
*/ 
private String downloadUrl(String strUrl) throws IOException { 
    String data = ""; 
    InputStream iStream = null; 
    HttpURLConnection urlConnection = null; 
    try { 
     URL url = new URL(strUrl); 

     // Creating an http connection to communicate with url 
     urlConnection = (HttpURLConnection) url.openConnection(); 

     // Connecting to url 
     urlConnection.connect(); 

     // Reading data from url 
     iStream = urlConnection.getInputStream(); 

     BufferedReader br = new BufferedReader(new InputStreamReader(iStream)); 

     StringBuffer sb = new StringBuffer(); 

     String line = ""; 
     while ((line = br.readLine()) != null) { 
      sb.append(line); 
     } 

     data = sb.toString(); 
     Log.d("downloadUrl", data.toString()); 
     br.close(); 

    } catch (Exception e) { 
     Log.d("Exception", e.toString()); 
    } finally { 
     iStream.close(); 
     urlConnection.disconnect(); 
    } 
    return data; 
} 

// Fetches data from url passed 
private class FetchUrl extends AsyncTask<String, Void, String> { 

    @Override 
    protected String doInBackground(String... url) { 

     // For storing data from web service 
     String data = ""; 

     try { 
      // Fetching the data from web service 
      data = downloadUrl(url[0]); 
      Log.d("Background Task data", data.toString()); 
     } catch (Exception e) { 
      Log.d("Background Task", e.toString()); 
     } 
     return data; 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     super.onPostExecute(result); 

     ParserTask parserTask = new ParserTask(); 

     // Invokes the thread for parsing the JSON data 
     parserTask.execute(result); 

    } 
} 

/** 
* A class to parse the Google Places in JSON format 
*/ 
private class ParserTask extends AsyncTask<String, Integer, List<List<HashMap<String, String>>>> { 

    // Parsing the data in non-ui thread 
    @Override 
    protected List<List<HashMap<String, String>>> doInBackground(String... jsonData) { 

     JSONObject jObject; 
     List<List<HashMap<String, String>>> routes = null; 

     try { 
      jObject = new JSONObject(jsonData[0]); 
      Log.d("ParserTask",jsonData[0].toString()); 
      DataParser parser = new DataParser(); 
      Log.d("ParserTask", parser.toString()); 

      // Starts parsing data 
      routes = parser.parse(jObject); 
      Log.d("ParserTask","Executing routes"); 
      Log.d("ParserTask",routes.toString()); 

     } catch (Exception e) { 
      Log.d("ParserTask",e.toString()); 
      e.printStackTrace(); 
     } 
     return routes; 
    } 

    // Executes in UI thread, after the parsing process 
    @Override 
    protected void onPostExecute(List<List<HashMap<String, String>>> result) { 
     ArrayList<LatLng> points; 
     PolylineOptions lineOptions = null; 
     String distance = ""; 
     String duration = ""; 

     // Traversing through all the routes 
     for (int i = 0; i < result.size(); i++) { 
      points = new ArrayList<>(); 
      lineOptions = new PolylineOptions(); 

      // Fetching i-th route 
      List<HashMap<String, String>> path = result.get(i); 

      // Fetching all the points in i-th route 
      for (int j = 0; j < path.size(); j++) { 
       HashMap<String, String> point = path.get(j); 

       if(j==0){ // Get distance from the list 
        distance = (String)point.get("distance"); 
        continue; 
       }else if(j==1){ // Get duration from the list 
        duration = (String)point.get("duration"); 
        continue; 
       } 

       double lat = Double.parseDouble(point.get("lat")); 
       double lng = Double.parseDouble(point.get("lng")); 
       LatLng position = new LatLng(lat, lng); 

       points.add(position); 
      } 

      // Adding all the points in the route to LineOptions 
      lineOptions.addAll(points); 
      lineOptions.width(10); 
      lineOptions.color(Color.RED); 

      Log.d("onPostExecute","onPostExecute lineoptions decoded"); 
      tvDistanceDuration.setText("Distance:"+distance + ", Duration:"+duration); 
     } 

     // Drawing polyline in the Google Map for the i-th route 
     if(lineOptions != null) { 
      mMap.addPolyline(lineOptions); 
     } 
     else { 
      Log.d("onPostExecute","without Polylines drawn"); 
     } 
    } 
+0

2つのAsynctasksは必要ありません... JSON解析はUIスレッド上で問題ありません。このコードは複雑である必要があります –

+0

大丈夫です、どうすれば距離を得ることができますか? – Jincy

+0

'LatLng'や' Location'クラスにはdistanceメソッド自体があると思います。ボタンは実際の距離計算とは無関係です –

答えて

0

両方の場所の緯度と経度を知っていれば、両方の位置の距離を簡単に計算できます。詳細については、http://www.geodatasource.com/developers/javaをご覧ください。

ここであなたがアンドロイドでそれを行う方法です。

いくつかのクラスに

private static double distance(double lat1, double lon1, double lat2, double lon2, String unit) { 
     double theta = lon1 - lon2; 
     double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta)); 
     dist = Math.acos(dist); 
     dist = rad2deg(dist); 
     dist = dist * 60 * 1.1515; 
     if (unit == "K") { 
      dist = dist * 1.609344; 
     } else if (unit == "N") { 
      dist = dist * 0.8684; 
     } 

     return (dist); 
    } 

    /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/ 
    /*:: This function converts decimal degrees to radians      :*/ 
    /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/ 
    private static double deg2rad(double deg) { 
     return (deg * Math.PI/180.0); 
    } 

    /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/ 
    /*:: This function converts radians to decimal degrees      :*/ 
    /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/ 
    private static double rad2deg(double rad) { 
     return (rad * 180/Math.PI); 
    } 

を次のようにあなたはキロたとえば距離は

int distance = Helper.distance(lat1,lon1,lat2,lon2,"K")その後、ヘルパークラスの静的メソッドである使用「K」についてdistance方法

を使用することができますメソッドを作成します

の "N"

これは何とかあなたに役立つことを願っています。

+0

ありがとうZeeshan、これはGoogleマップで可能ですか? – Jincy

+0

Googleマップを使用して1位を取得し、2位の座標を位置指定してから、距離を見つけたらこれらの方法で距離を見つけて、どこにでも表示できます。 –

0

これは、ユーザーの場所から選択した場所までの距離と時間を取得するために使用する方法です。あなたのAsyncTaskの下にそれを追加します。このようなdoInBackground方法であなたのParserTask

public void getDistAndDuration(JSONObject jObj){ 
    try{ 
     JSONArray array = jObj.getJSONArray("routes"); 
     JSONObject rou = array.getJSONObject(0); 
     JSONArray legs = rou.getJSONArray("legs"); 
     JSONObject steps = legs.getJSONObject(0); 
     JSONObject distance = steps.getJSONObject("distance"); 
     JSONObject duration = steps.getJSONObject("duration"); 
     final String textDist = distance.getString("text"); 
     final String textDur = duration.getString("text"); 

     Log.i("DISTANCE", ""+textDist); 
     Log.i("TIME", ""+textDur); 

    }catch(Exception e){ 
     e.printStackTrace(); 
    } 
} 

は、このメソッドを呼び出し:

@Override 
protected List<List<HashMap<String, String>>> doInBackground(String... jsonData) { 

    JSONObject jObject; 
    List<List<HashMap<String, String>>> routes = null; 

    try { 
     jObject = new JSONObject(jsonData[0]); 

     ////////// HERE ////////////// 
     getDistAndDuration(jObject); 
     ////////////////////////////// 

     Log.d("ParserTask",jsonData[0].toString()); 
     DataParser parser = new DataParser(); 
     Log.d("ParserTask", parser.toString()); 

     // Starts parsing data 
     routes = parser.parse(jObject); 
     Log.d("ParserTask","Executing routes"); 
     Log.d("ParserTask",routes.toString()); 

    } catch (Exception e) { 
     Log.d("ParserTask",e.toString()); 
     e.printStackTrace(); 
    } 
    return routes; 
} 

あなただけのボタンをクリック上の距離を取得したい場合は、あなたのjObjectがグローバル宣言し、ボタンのクリックリスナーでgetDistAndDuration(JSONObject jObj)に電話してください。 AsyncTaskを最初に呼び出した後に必ず呼び出してください。

希望します:)

関連する問題