[android-developers] Can't show markers on android google maps

2016-07-06 Thread alessandro schirinzi
Hello. this is my code of MapsActivity.java, and I don't see my markers 
from mysql database. The error in logcat is:

Value 
[{"LAT":"41.1172905","LON":"16.4837252"},{"LAT":"41.1171432","LON":"16.8718715"},{"LAT":"40.8476509","LON":"15.5404692"},{"LAT":"48.856614","LON":"2.3522219"},{"LAT":"40.8517746","LON":"14.2681244"}]
 
of type org.json.JSONArray cannot be converted to JSONObject

The error shows the json result of my php code. This php page is the url 
string passed to get coordinates and markers.

Please help me. Nice thanks

This the java code:

public class MapsActivity extends FragmentActivity implements 
OnMapReadyCallback {

private GoogleMap mMap;
private Double Latitude = 0.00;
private Double Longitude = 0.00;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// 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);

new JSONTask().execute("http://192.168.1.101/crud/markers_android.php;);
}

@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;

}

public class JSONTask extends AsyncTask {

@Override
protected String doInBackground(String... params) {
HttpURLConnection connection = null;
BufferedReader reader = null;

try {
URL url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();

InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));

StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line);
}

String finalJson = buffer.toString();
JSONObject parentObject = new JSONObject(finalJson);
JSONArray parentArray = new JSONArray();

//;
//JSONArray parentArray = parentObject.getJSONArray("");

for (int i = 0; i

-- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to android-developers+unsubscr...@googlegroups.com.
To post to this group, send email to android-developers@googlegroups.com.
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/android-developers/21d13630-d4bb-42e4-8b09-89924286449d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[android-developers] android studio add markers to google maps from sqlite database

2016-06-28 Thread alessandro schirinzi
In my android app I'm trying to add several markers from sqlite to a 
MapsActivity. I created a do-while cycle for getting all the cities in 
sqlite table and convert all cities in coordinates (lat, long), but there's 
something in error. Can you help me? Thanks to all

MapsActivity.java

public class MapsActivity extends FragmentActivity implements 
OnMapReadyCallback {

private GoogleMap mMap;
DatabaseHelper myDb;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// 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);}
@Overridepublic void onMapReady(GoogleMap googleMap) {
mMap = googleMap;

// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
LatLng ruvo = new LatLng(-45, 123);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in 
Sydney"));
mMap.addMarker(new MarkerOptions().position(ruvo).title("Marker in 
Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(ruvo));
displayCities();}
public void onSearch(View view) {
EditText textCity = (EditText) findViewById(R.id.textCity);
String location = textCity.getText().toString();
List addressList = null;

if (location != null) {
Geocoder geocoder = new Geocoder(this);
try {
addressList = geocoder.getFromLocationName(location, 1);
} catch (IOException e) {
e.printStackTrace();
}
android.location.Address address = addressList.get(0);
LatLng latLng = new LatLng(address.getLatitude(), 
address.getLongitude());
mMap.addMarker(new MarkerOptions().position(latLng).title("Marker 
pointed"));
mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
}
}
public void displayCities() {
List addressList = null;
Cursor database = myDb.display();

if (database.getCount()== 0) {
Toast.makeText(this, "Nothing to found", Toast.LENGTH_LONG).show();
return;
}
//string array of all cities
database.moveToFirst();
String[] cities = new String[database.getCount()];
int n = 0;
do {
cities[n] = database.getString(3).toString();
n=n+1;
} while (database.moveToNext());

Geocoder geocoder = new Geocoder(this);
try {
addressList = geocoder.getFromLocationName(cities[n], 1);
} catch (IOException e) {
e.printStackTrace();
}
android.location.Address address = addressList.get(0);
LatLng latLng = new LatLng(address.getLatitude(), 
address.getLongitude());
mMap.addMarker(new MarkerOptions().position(latLng).title("Marker 
pointed"));
}

}

DataBaseHelper.java

public class DatabaseHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "Student.db";public static final 
String TABLE_NAME = "student_table";//VERY IMPORTANT: CursorAdapter needs 
exactly "_id" column, as written herepublic static final String COL_1 = 
"_id";public static final String COL_2 = "NAME";public static final String 
COL_3 = "SURNAME";public static final String COL_4 = "CITY";
public ArrayList results = new ArrayList<>();
public DatabaseHelper(Context context, String name, 
SQLiteDatabase.CursorFactory factory, int version) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);}
@Overridepublic void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME + " ( " + COL_1 + " INTEGER PRIMARY 
KEY AUTOINCREMENT , "
+ COL_2 + " TEXT , " + COL_3 + " TEXT , " + COL_4 + " TEXT );");}
@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int 
newVersion) {
db.execSQL("DROP TABLE IF EXISTS" + TABLE_NAME);
onCreate(db);}
public Cursor selected(long id) {
SQLiteDatabase db = this.getWritableDatabase();
String[] columns = new String[]{DatabaseHelper.COL_1, DatabaseHelper.COL_2, 
DatabaseHelper.COL_3, DatabaseHelper.COL_4};
Cursor c = db.query(DatabaseHelper.TABLE_NAME, columns, 
DatabaseHelper.COL_1 + "=" + id, null, null, null, null);
if (c != null) {
c.moveToFirst();
}
return c;}
public boolean insertData(String name, String surname, String city) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues ContentValues = new ContentValues();
ContentValues.put(COL_2, name);
ContentValues.put(COL_3, surname);
ContentValues.put(COL_4, city);
long result = db.insert(TABLE_NAME, null, ContentValues);
if (result == -1)
return false;
else
return true;}
//method for select querypublic Cursor display() {
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery("select * from "