php - 如何在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 循环中可能有错误
谁能让我摆脱这种情况。
解决方案
您正在此行中添加另一层数组
json_encode(array($flag));
将其更改为
json_encode($flag);
除非您在返回的 JSONString 中明确想要另一个级别的数组
推荐阅读
- python - Django Rest - 序列化器:必须是创建时的实例
- mysql - 每月统计客户无法起诉 Laravel 查询生成器
- windows - 在 Windows 10 中以高对比度的暗模式使用 Visual Studio 2019
- rancher - Rancher CLI 登录错误 - 状态 [401 未授权]。正文:[message=clusterID 不匹配]
- c# - 如何从 Windows 窗体应用程序写入文本文件
- r - Mplus vs lavaan 报告的自由参数数量
- javascript - 托管 let, const
- parse-platform - 如何以编程方式在 Parse Server 中的 CLP 上设置指针权限?
- pygame - 如何将多首歌曲/曲目加载到 pygame 中?
- google-apps-script - 调用方法 guestCanModify() 时出错