首页 > 解决方案 > 如何在android中使用php添加多个标记

问题描述

裸露我的英语

代码 :

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 循环中可能有错误

谁能让我摆脱这种情况。

标签: phpandroidjsongoogle-maps

解决方案


您正在此行中添加另一层数组

json_encode(array($flag)); 

将其更改为

json_encode($flag); 

除非您在返回的 JSONString 中明确想要另一个级别的数组


推荐阅读