|
|
|
รบกวนสอบถาม เรื่องการดึงข้อมูลจาก mysql to map ของ Android หน่อยครับ ว่า ตรงไหนผิดไปหรือป่าว |
|
|
|
|
|
|
|
รบกวนสอบถามหน่อยครับ
พอดีว่ากำลังทดสอบแอฟอยู่ครับ ทำอย่างไรก็ไม่ติดสักที ไม่รู้ว่าผิดพลาดตรงไหน
ไฟล์ json ที่โหลดมานะครั บเป็นแบบนี้
Code
[{"fname":"TTI","lat":"13.7522222","lng":"100.4938889"},{"fname":"Patrick","lat":"16.815833","lng":"100.263611"},{"fname":"Dobsob","lat":"13.956551","lng":"100.62128"}]
Code (Android-Java)
public class GPSMap4Activity extends FragmentActivity implements OnMapReadyCallback{
GoogleMap mGoogleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gpsmap4);
MapFragment mapFragment = (MapFragment)getFragmentManager().findFragmentById(R.id.map2);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
try {
JSONArray data = new JSONArray(getJSON());
String[] fullName = new String[data.length()];
Double[] latitude = new Double[data.length()];
Double[] longtitude = new Double[data.length()];
for (int i = 0; i<data.length();i++) {
JSONObject c = data.getJSONObject(i);
fullName[i] = c.getString("fname");
latitude[i] = c.getDouble("lat");
longtitude[i] = c.getDouble("lng");
googleMap.addMarker(new MarkerOptions()
.title(fullName[i])
.position(new LatLng(latitude[i], longtitude[i])));
}
LatLng thailand = new LatLng(13.75000,100.466667);
googleMap.setMyLocationEnabled(true);
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(thailand, 13));
googleMap.addMarker(new MarkerOptions()
.title("Thailand")
.snippet("The most populous city in Thatland.")
.position(thailand));
} catch (JSONException e){
e.printStackTrace();
}
}
public String getJSON() {
String strUrl = "http://27.254.143.125:85/~gpslocation/db_selecttrackinggps2.php";
URL url =null;
StringBuffer sb= new StringBuffer();
try {
url = new URL(strUrl);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.connect();
InputStream iStream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(iStream));
String line = "";
while((line = reader.readLine()) != null){
sb.append(line);
}
reader.close();
iStream.close();
}catch (MalformedURLException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
return sb.toString();
}
}
Tag : Mobile, Android
|
|
|
|
|
|
Date :
2015-05-13 20:01:19 |
By :
Kamonth |
View :
990 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Android and JSON
มีตัวอย่างที่เกี่ยวข้องด้วยครับ
|
|
|
|
|
Date :
2015-05-14 10:06:59 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 03
|