|
|
|
Android สอบถามเรื่องการดึงข้อมูลมาทำ Marker จาก MySQL มาแสดงบน Google map ใน Fragment หน่อยค่ะ |
|
|
|
|
|
|
|
หลังจากที่ได้ทดลองแก้โค้ดมาซักระยะ marker ก็ยังไม่ขึ้นค่ะ
เมื่อเรียกใช้ latitude longitude จากตัวแปรมันไม่แสดงแผนที่น่ะค่ะ
แจ้ง Unfortunately, ... has stopped. ตลอดเลย
รบกวนช่วยตอบหน่อยนะคะ ขอบคุณค่ะ
fragment2getlocal.php
<?php
require "testsqls.php";
$mysql_qry = "select * from machine";
$result = mysqli_query($conn,$mysql_qry);
$response = array();
while($row = mysqli_fetch_array($result))
{
array_push($response,array("machinid"=>$row[0],"latitude"=>$row[1],"longitude"=>$row[2]));
}
echo json_encode(array("result"=>$response));
mysqli_close($conn);
?>
fragment_fragmentmap.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.alertfiretankforest.firetank.Fragmentmap">
<com.google.android.gms.maps.MapView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map"
/>
</FrameLayout>
Fragmentmap.java
public class Fragmentmap extends Fragment implements OnMapReadyCallback {
GoogleMap mGoogleMap;
MapView mMapView;
View mView;
ArrayList<String> machinid;
ArrayList<Double> latitude;
ArrayList<Double> longitude;
int unit;
public Fragmentmap() {
// Required empty public constructor
}
@Override
public void onCreate( Bundle savedInstanceState) {super.onCreate(savedInstanceState); }
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mView= inflater.inflate(R.layout.fragment_fragmentmap, container, false);
machinid= new ArrayList<String>();
latitude=new ArrayList<Double>();
longitude=new ArrayList<Double>();
// Inflate the layout for this fragment
new AsyncTask<Void, Void, Void>() {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
try {
URL url = new URL("http://192.168.0.87/fragment2getlocal.php");
URLConnection urlConnnection=url.openConnection();
HttpURLConnection httpURLConnection=(HttpURLConnection)urlConnnection;
httpURLConnection.setAllowUserInteraction(false);
httpURLConnection.setInstanceFollowRedirects(true);
httpURLConnection.setRequestMethod("GET");
httpURLConnection.connect();
InputStream inputStream = null;
if(httpURLConnection.getResponseCode()==HttpURLConnection.HTTP_OK)
{
inputStream=httpURLConnection.getInputStream();
}
BufferedReader reader= new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"),8);
//ส่งค่าที่อ่านไป String Viewder
StringBuilder stringBuilder=new StringBuilder();
String line=null;
while ((line=reader.readLine()) != null)
{
stringBuilder.append(line+"\n");
}
inputStream.close();
Log.d("JSON Result",stringBuilder.toString());
JSONObject jsonObject=new JSONObject(stringBuilder.toString());
JSONArray exArray=jsonObject.getJSONArray("result");
unit=exArray.length();
for (int i=0;i<exArray.length();i++)
{
JSONObject jsonObj =exArray.getJSONObject(i);
machinid.add(jsonObj.getString("machinid"));
latitude.add(jsonObj.getDouble("latitude"));
longitude.add(jsonObj.getDouble("longitude"));
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(Void aVoid) { }
}.execute();
return mView;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mMapView=(MapView)mView.findViewById(R.id.map);
if (mMapView != null)
{
mMapView.onCreate(null);
mMapView.onResume();
mMapView.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap=googleMap;
mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude.get(0),longitude.get(0)),18));
mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
for(int i=0;i<machinid.size();i++){
MarkerOptions marker=new MarkerOptions().position(new LatLng(latitude.get(0),longitude.get(0))).title("New");
mGoogleMap.addMarker(marker);
}
}
}
Tag : Mobile, MySQL, Android, JAVA
|
|
|
|
|
|
Date :
2017-01-28 15:08:11 |
By :
Sirinan008 |
View :
1382 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Android Google Map : Marker Location from PHP/MySQL (JSON)
Android Google Map : Adding Multiple Marker (Latitude, Longitude)
|
|
|
|
|
Date :
2017-01-30 11:19:23 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 05
|