Bare my English
Код:
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
Может кто-нибудь меня вывести изэта ситуация.