【发布时间】:2019-03-14 02:32:57
【问题描述】:
裸露我的英语
代码:
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 循环中可能有错误
谁能让我摆脱这种情况。
【问题讨论】:
-
如果您向我们展示您的 PHP 代码
location.php会很有帮助 -
lat = data.getDouble( "latitude" ),使用 Double 会使您失去精度,然后无法将标记设置在正确的位置。
-
看看我现在更新了我的代码@RiggsFolly
-
谢谢你成功了@RiggsFolly
标签: php android json google-maps