从 WAMP sql 服务器数据库提取数据到 Android 应用程序

Extract data from WAMP sql server database to Android App

我的目标是根据从 WAMP 服务器接收到的 GPS 坐标绘制标记。我无法绘制标记,我应该在我的代码中改进什么才能做到这一点?

以下是负责从 WAMP 服务器上的数据库 运行 获取数据并在地图上绘制标记的代码:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
    private GoogleMap mMap;
    private RequestQueue requestQueue;
    private double lat;
    private double lon;
    private int flag;
    private String showUrl = "http://<<LOCAL-IP-ADDRESS>/directory/showSensor.php";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // 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);

        requestQueue = Volley.newRequestQueue(getApplicationContext());
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, showUrl,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            JSONArray sensorLocations = response.getJSONArray("Sensor Locations");

                            for (int i = 0; i < sensorLocations.length(); i++) {
                                JSONObject sensorLocation = sensorLocations.getJSONObject(i);
                                String latitude = sensorLocation.getString("lat");
                                String longitude = sensorLocation.getString("long");
                                String flg = sensorLocation.getString("flag");

                                if (flag == 1) {
                                    lat = Double.parseDouble(latitude);
                                    lon = Double.parseDouble(longitude);
                                    flag = 0;
                                }
                            }
                            ;
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                    }
                });
        requestQueue.add(jsonObjectRequest);
    }

    /**
     * Manipulates the map once available. This callback is triggered when the
     * map is ready to be used. This is where we can add markers or lines, add
     * listeners or move the camera. In this case, we just add a marker near
     * Sydney, Australia. If Google Play services is not installed on the
     * device, the user will be prompted to install it inside the
     * SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        // Add a marker in Sydney and move the camera
        LatLng sensor = new LatLng(lat, lon);
        mMap.addMarker(new MarkerOptions().position(sensor).title(lat + "," + lon));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sensor));
    }
}

似乎 onMapReady 方法在您收到服务器响应之前被调用。将代码从您的服务器请求数据移动到 onMapReady 方法:

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    requestQueue = Volley.newRequestQueue(getApplicationContext());
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,
                showUrl, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {

                try {
                    JSONArray sensorLocations = response.getJSONArray("Sensor Locations");

                    for (int i = 0; i < sensorLocations.length(); i++) {

                        JSONObject sensorLocation  = sensorLocations.getJSONObject(i);
                        String latitude = sensorLocation.getString("lat");
                        String longitude= sensorLocation.getString("long");
                        String flg = sensorLocation.getString("flag");

                        if(flag ==1 ) {
                            lat = Double.parseDouble(latitude);
                            lon = Double.parseDouble(longitude);
                            flag=0;

                            LatLng sensor = new LatLng(lat, lon);
                            mMap.addMarker(new MarkerOptions().position(sensor).title(lat+ "  " + lon));
                            mMap.moveCamera(CameraUpdateFactory.newLatLng(sensor));
                        }
                    }

                    ;
                }catch(JSONException e){
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
    requestQueue.add(jsonObjectRequest);
}