Take mysql data and use in Mapsfragment on Android

Asked

Viewed 342 times

0

My part of php I believe is working. I don’t know how to get this data to Android. How can I do? I need to pass the name data,message,lat,long for some variable on android.

Part in 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>";
}
?>

Part 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));

}
}

1 answer

1


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

JSON

THE 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 on a JSON, with the functions json_encode and json_decode and prevent us from possible mistakes with the json_last_error.

JSON is a structure for representation of Javascript data

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

Example

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

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

Details

  • I’m novice in php and android, could show me how I do it?

  • @Marcoskropp I put some references to you to better understand. Read this article and you will learn more: http://mariohercules.blogspot.com.br/2012/04/utilizando-mysql-e-php-para-gerar-json.html

  • @Marcoskropp we are already in contact! Questions can be taken from here. And I’m also in my job, I can only answer when there’s some time left.

  • So, I do not know how to make the code, the way I did I can receive the data from the Database, but I do not know how to return it to Android, and I need to return this data to Mapsfragment which is all different from other.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.