Androidでサーバー内のGPS情報をjsonで受け取り、地図にマーカーで表示するアプリを作成しています。
しかし、地図上にマーカーを表示できずに困っております。
GPS情報が書かれているjsonは、
json
1[{"id":"1","title":"野々市小学校","address":"野々市市本町5-3-1","lat":"36.5319906800","lng":"136.6092836000"}]
Androidのコードは、
MapsActivity.java
java
1public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { 2 private GoogleMap mMap; 3 String url = "http://ambient.naist.jp/hoge/fuga/map_data.php"; 4 5 String title=""; 6 String address=""; 7 String lat = ""; 8 String lng = ""; 9 10 private List<Refuge> mRefugeList = new ArrayList<Refuge>(); 11 12 @Override 13 protected void onCreate(Bundle savedInstanceState) { 14 super.onCreate(savedInstanceState); 15 setContentView(R.layout.activity_maps); 16 SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 17 .findFragmentById(R.id.map); 18 mapFragment.getMapAsync(this); 19 } 20 21 @Override 22 protected void onResume() { 23 super.onResume(); 24 } 25 26 @Override 27 public void onMapReady(GoogleMap googleMap) { 28 mMap = googleMap; 29 mRefugeList.clear(); 30 parseJSON(); 31 addMaker(); 32 33 mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() { 34 @Override 35 public void onMapClick(LatLng latLng) { 36 calcDistance(latLng); 37 sortRefugeList(); 38 updateMaker(); 39 addLine(latLng); 40 } 41 }); 42 } 43 44 private void addLine(LatLng point){ 45 CircleOptions circleOptions = new CircleOptions() 46 .center(point) 47 //.fillColor(Color.LTGRAY) 48 .radius(3); 49 mMap.addCircle(circleOptions); 50 for (Refuge refuge : mRefugeList) { 51 if (refuge != null) { 52 if (refuge.isNear()) { 53 PolylineOptions polyOptions = new PolylineOptions(); 54 polyOptions.add(point); 55 polyOptions.add(new LatLng(refuge.getLat(), refuge.getLng())); 56 polyOptions.color(Color.GRAY); 57 polyOptions.width(3); 58 polyOptions.geodesic(true); 59 mMap.addPolyline(polyOptions); 60 } 61 } 62 } 63 } 64 65 private void updateMaker() { 66 int i = 0; 67 mMap.clear(); 68 for (Refuge refuge : mRefugeList) { 69 if (refuge != null) { 70 MarkerOptions options = new MarkerOptions(); 71 options.position(new LatLng(refuge.getLat(), refuge.getLng())); 72 options.title(refuge.getName() + " " + refuge.getDistance() + "m"); 73 options.snippet(refuge.getAddress()); 74 BitmapDescriptor icon; 75 if (i > 2) { 76 icon = BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA); 77 refuge.setNear(false); 78 } else { 79 icon = BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED); 80 refuge.setNear(true); 81 } 82 options.icon(icon); 83 Marker marker = mMap.addMarker(options); 84 if (i == 0) { 85 marker.showInfoWindow(); 86 } 87 i++; 88 } 89 } 90 } 91 private void sortRefugeList(){ 92 Collections.sort(mRefugeList, new Comparator<Refuge>() { 93 @Override 94 public int compare(Refuge lhs, Refuge rhs) { 95 return lhs.getDistance() - rhs.getDistance(); 96 } 97 }); 98 } 99 private void calcDistance(LatLng point){ 100 // タッチした場所と避難所の距離を求める 101 double startLat = point.latitude; 102 double startLng = point.longitude; 103 // 結果を格納するための配列 104 float[] results = new float[3]; 105 for (Refuge refuge : mRefugeList) { 106 if (refuge != null) { 107 Location.distanceBetween(startLat, startLng, refuge.getLat(), refuge.getLng(), results); 108 refuge.setDistance(results[0]); 109 } 110 } 111 112 113 } 114 115 private void parseJSON(){ 116 //受け取るjsonの最初が配列[]の場合JSONObjectをJSONArrayに変更する 117 JsonArrayRequest jsArrReuest = new JsonArrayRequest( 118 Request.Method.POST, 119 url, 120 null, 121 new Response.Listener<JSONArray>(){ 122 @Override 123 public void onResponse(JSONArray response){ 124 try{ 125 title = response.getJSONObject(0).getString("title"); 126 address = response.getJSONObject(0).getString("address"); 127 lat = response.getJSONObject(0).getString("lat"); 128 lng = response.getJSONObject(0).getString("lng"); 129 newRefuge(title, address,Double.valueOf(lat), Double.valueOf(lng)); 130 } catch (JSONException e) { 131 e.printStackTrace(); 132 } 133 } 134 }, 135 new Response.ErrorListener(){ 136 @Override 137 public void onErrorResponse(VolleyError error){ 138 Log.d(TAG, error.toString()); 139 } 140 } 141 ); 142 MySingleton.getInstance(this).addToRequestQueue(jsArrReuest); 143 } 144 145 private void newRefuge(String title,String address,double lat,double lon) { 146 Refuge refuge; 147 refuge = new Refuge(title,address,lat,lon); 148 mRefugeList.add(refuge); 149 } 150 151 private void addMaker() { 152 for (Refuge refuge : mRefugeList) { 153 if (refuge != null) { 154 MarkerOptions options = new MarkerOptions(); 155 options.position(new LatLng(refuge.getLat(),refuge.getLng())); 156 options.title(refuge.getName()); 157 options.snippet(refuge.getAddress()); 158 mMap.addMarker(options); 159 } 160 } 161 } 162}
Refuge.java
java
1public class Refuge { 2 private String name; 3 private String address; 4 private double latitude; 5 private double longitude; 6 private boolean near; 7 private int distance; 8 9 public Refuge(String name,String address,double latitude,double longitude){ 10 this.name = name; 11 this.address = address; 12 this.latitude = latitude; 13 this.longitude = longitude; 14 this.near = false; 15 this.distance = 0; 16 } 17 public String getName() { 18 return name; 19 } 20 public String getAddress() { 21 return address; 22 } 23 public double getLat() { 24 return latitude; 25 } 26 public double getLng() { 27 return longitude; 28 } 29 public boolean isNear() { 30 return near; 31 } 32 public int getDistance() { 33 return distance; 34 } 35 public void setNear(boolean near) { 36 this.near = near; 37 } 38 public void setDistance(float distance) { 39 this.distance = (int)distance; 40 } 41}
Log.dでサーバーからのjson取得は確認できています。
しかし、mRefugeListが何も取得できていない状態になっています。
何か良い方法があれば教えていただけないでしょうか。
宜しくお願い致します。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2016/10/18 02:26
2016/10/18 02:40
退会済みユーザー
2016/10/18 03:10
2016/10/18 04:34
退会済みユーザー
2016/10/18 04:57
2016/10/18 05:03
退会済みユーザー
2016/10/18 05:16