Get mysql data and use in MapsFragment on Android

0

My share of php I believe is working. I do not know how to get this data for Android. How can I do? I need to pass the data name, message, lat, long to some variable in android.

Part of Php:

    $sql = $dbcon->query("SELECT * FROM mensagem 
     INNER JOIN usuarios ON mensagem.idUsuarios = usuarios.idUsuarios

");

foreach ($sql as $value) {
//$sql = $value['mensagem'];
echo $value['nome']; 
echo "<br>";
echo $value['mensagem'];
echo "<br>";
echo $value['lat'];
echo "<br>";
echo $value['long'];
echo "<br>";
}
?>

Share on Android:

package mundosenai.mundosenai.com.mundosenai;

import android.support.v4.app.FragmentActivity;
import android.os.Bundle;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import android.content.Intent;
import android.widget.Button;

public class MapsFragment extends SupportMapFragment implements OnMapReadyCallback {

private GoogleMap mMap;





@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getMapAsync(this);
}

double lat;
double longi;
String mensagem;
String nome;





@Override
public void onMapReady(GoogleMap map) {
    LatLng MinhaLocalizacao = new LatLng(lat, longi);



    map.moveCamera(CameraUpdateFactory.newLatLngZoom(MinhaLocalizacao, 3));

}
}
    
asked by anonymous 21.09.2016 / 02:15

1 answer

1

There are a few distinct ways that you can solve your problem by returning the same result using JSON or XML. At first it is necessary to structure the information returned from PHP in a compact way in one of these ways. I will show below how it would be using JSON.

JSON

JSON (JavaScript Object Notation) is a template for storing and transmitting information in text format.

From PHP version 5.2 it is possible to transform a Array into a JSON, with the functions json_encode and json_decode and prevent possible errors with json_last_error .

  

JSON is a framework for rendering data in JavaScript

  • json_decode - Decode a JSON string
  • json_encode - Returns the JSON representation of a value
  • json_last_error_msg - Returns a string containing the error message of - last call of json_encode () or json_decode ()
  • json_last_error - Returns the last error occurred

Example

{"latitude": -25.6355281, "longitude": 45.8824533}

Step two, it would be the consumption of JSON with Android using HTTP, which also there are several tools for your case. Here in this article , has a good example of how you can do that.

Details

21.09.2016 / 15:15