如何在Android中使用PHP添加多个标记

时间:2018-10-09 10:32:07

标签: php android json google-maps

不懂我的英语

代码:

public class Map extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;
double lat;
double longi;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate( savedInstanceState );
    setContentView( R.layout.activity_map );
    // 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 );
}
@Override
public void onMapReady(GoogleMap googleMap) {
    new Getplace().execute();
    mMap = googleMap;

}

private class Getplace extends AsyncTask<String, Void, String> {

    public static final int CONNECTION_TIMEOUT = 10000;
    public static final int READ_TIMEOUT = 15000;
    HttpURLConnection conn;
    URL url = null;

    @Override
    protected String doInBackground(String... strings) {

        try {
            url = new URL( "http://localhost:8089/location.php" );
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

        try {
            conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout( READ_TIMEOUT );
            conn.setConnectTimeout( CONNECTION_TIMEOUT );
            conn.setRequestMethod( "POST" );
            // setDoOutput to true as we recieve data from json file
            conn.setDoOutput( true );
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {

            int response_code = conn.getResponseCode();

            // Check if successful connection made
            if (response_code == HttpURLConnection.HTTP_OK) {

                // Read data sent from server
                InputStream input = conn.getInputStream();
                BufferedReader reader = new BufferedReader( new InputStreamReader( input ) );
                StringBuilder result = new StringBuilder();
                String line;

                while ((line = reader.readLine()) != null) {
                    result.append( line );
                }

                // Pass data to onPostExecute method
                return (result.toString());

            } else {

                return ("unsuccessful");
            }

        } catch (IOException e) {
            e.printStackTrace();
            return e.toString();
        } finally {
            conn.disconnect();
        }
        }
    @Override
    protected void onPreExecute() {
    }
    @Override
    protected void onPostExecute(String result) {
        Log.e( TAG,"result"+result );
        try {
            JSONArray row = new JSONArray( result );
            for(int i=0;i<row.length();i++){
                JSONObject data = row.getJSONObject(i);
                lat = data.getDouble( "latitude" );
                longi = data.getDouble( "longitude" );
                LatLng sydney = new LatLng( lat, longi );

                mMap.addMarker( new MarkerOptions().position( sydney ));
            }
        }catch (JSONException e) {
        }
    }
}

}

Php代码:

<?php

$host='localhost';

$uname='root';

$pwd='';

$db="nikhil";

$con = mysqli_connect($host,$uname,$pwd,$db);

if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}


$query=mysqli_query($con, "select * from Location"); 

while($row=mysqli_fetch_array($query))
{
$flag[]=$row;
}
echo json_encode(array($flag));  //json output

mysqli_close($con);
?>

结果:

1。纬度:23.8103,经度:90.4125

2。纬度:22.7010,经度:90.3535

3。纬度:23.6,经度:89.8333333

我要在地图上添加多个标记。

还有我为此编写的代码,我得到的地图没有任何标记。 我可以正确地获取我的经度和纬度,但是我想它们并没有分配给标记。

还有一件事:

Log.e( TAG,"No :"+i+" Latitue :"+lat );

我在logcat中看不到此行,所以我的for循环中可能存在错误

我能摆脱这种情况吗?

1 个答案:

答案 0 :(得分:0)

您要在此行中添加另一个级别的数组

json_encode(array($flag)); 

将其更改为

json_encode($flag); 

除非您明确表示要在返回的JSONString中使用另一级数组