2016-09-11 22 views
-3

私はjson URLから私の近くにある場所を取得しようとしていますが、私は最も近い場所に行くことができるので、 このように、私はonLocationChanged内で緯度と経度を取得するメソッドを呼び出しています。私の場所が変更されたときにJSonから場所を取得

GoogleMap mGoogleMap; 
MapView mapFrag; 
LocationRequest mLocationRequest; 
GoogleApiClient mGoogleApiClient; 
Location mLastLocation; 

public void onLocationChanged(Location location) 
{ 
    mLastLocation = location; 

    LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude()); 
    MarkerOptions markerOptions = new MarkerOptions(); 
    markerOptions.position(latLng); 
    loadJson(location.getLatitude(),location.getLongitude()); 

    mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 
    mGoogleMap.animateCamera(CameraUpdateFactory.zoomTo(11)); 

    if (mGoogleApiClient != null) { 
     LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this); 
    } 
} 

問題は、私はREQUESTQUEUEとStringRequest、innerclassのResponse.ListenerからメソッドonResonse到達しませんを使用して、JSON配列を取るしようとしている方法です。 そして、私はマップにマーカーを追加しています。

private void loadJson(double lat, double longit){ 
     lat = -22.950491; 
     longit= -43.181609; 

     // Store values at the time of the login attempt. 
     RequestQueue queue = Volley.newRequestQueue(this); 
     String url = "http://MyJsonURL"; 
     StringRequest stringRequest = new StringRequest(Request.Method.GET, url, 
       new Response.Listener<String>() { 
        @Override 
        public void onResponse(String response) { 
         try { 
          List<Places> arrayList = new ArrayList<Places>(); 
          JSONArray jsonArray = new JSONArray(response); 
          for(int i = 0; i< jsonArray.length();i++){ 
           if ((jsonArray.getJSONObject(i).optString("latitude") != "") && (jsonArray.getJSONObject(i).optString("longitude") != "")){ 
            Places places = new Places(); 
            places.setLatitude(jsonArray.getJSONObject(i).getDouble("latitude")); 
            places.setLongitude(jsonArray.getJSONObject(i).getDouble("longitude")); 
                     places.setNome(jsonArray.getJSONObject(i).getString("nome")); 
            mGoogleMap.addMarker(new MarkerOptions().position(new LatLng(places.getLatitude(),escolasMaps.getLongitude())).title(places.getNome())); 
           } 

          } 
         } catch (JSONException e) { 
          e.printStackTrace(); 
         } 
        } 
       }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       String err = (error.getMessage()==null)?"SD Card failed":error.getMessage(); 
       Log.e("sdcard-err2:",err); 
      } 
     }); 

     queue.add(stringRequest); 
     queue.start(); 
    } 

私は解決するために取るしたいもう一つの問題は、私は、マーカーのタイトルのエントリに値を置くことができますので、私はそれをクリックしたときに、私が代わりにエントリを値を送信する方法です。

+0

私は 'http:// MyJsonURL'があなたのリクエストに応答するつもりはないと思います。私は、なぜlogcatがSDから何かを取得しようとしていると言っているのか理解していません。回答を得たい場合は、コピーしたコードの質問を止めてください – Chisko

答えて

0

問題はqueue.start()で発生しました;これは1つのスレッドの中断を引き起こしていました。私はこの呼び出しを削除し、要求の時間を増やすためにタイムアウトのサイズを広げなければなりませんでした。 これは問題を解決します。

関連する問題