I'm using this below to add map markers to a Google Map. I have a table column FIELD_NAME. There are several locations each tied to a same FIELD_NAME. So for example there may be 5 entries of FIELD_NAME Woods with each entry having its own latitude/long value. I'm trying to figure out how to query the table to provide the FIELD_NAME but I only want it to show one of each name, not every time it is used. Once I get the name I will use it to display all the lat long points of that selected FIELD_NAME. How do you do this and does anyone have any links to tutorials on queries of this type?
#Override
public void onLoadFinished(Loader<Cursor> arg0,
Cursor arg1) {
int locationCount = 0;
double lat=0;
double lng=0;
// Number of locations available in the SQLite database table
locationCount = arg1.getCount();
// Move the current record pointer to the first row of the table
arg1.moveToFirst();
for(int i=0;i<locationCount;i++){
// Get the latitude
name = arg1.getString(arg1.getColumnIndex(LocationsDB.FIELD_NAME));
lacomments = arg1.getString(arg1.getColumnIndex(LocationsDB.FIELD_COMMENTS));
lat = arg1.getDouble(arg1.getColumnIndex(LocationsDB.FIELD_LAT));
// Get the longitude
lng = arg1.getDouble(arg1.getColumnIndex(LocationsDB.FIELD_LNG));
// Creating an instance of LatLng to plot the location in Google Maps
locationEngine = new LatLng(lat, lng);
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(locationEngine);
// Adding marker on the Google Map
getMap().addMarker(markerOptions);
arg1.moveToNext();
}
}
if you want to populate distinct FIELD_NAME in some spinner and then on selection of it display all the location for that, better do it with 2 queries
bind the spinner with result of
"select distinct FIELD_NAME from tablename"
and then once user provides his selection, use the below query
select * from tablename where FIELD_NAME ='whatever_user_selected'
Note: tablename - replace with your table's name
Let me know if there is not UI for user selection and you want random
entries for a specific field
Related
I'm working on an application where I have a ListView that shows the coordinates of my smartphone's position previously stored on a database. The rows of the ListView appear to be like: (latitude, longitude), date.
Is it possible to get the single values of the item? I was thinking to retrieve only the latitude and longitude values of the clicked item so that I can open a new MapsActivity that sets a marker at those specific coordinates.
Looking through other similar questions I've got that with ((TextView) view).getText() or with listView.getItemAtPosition(position) I can read the entire content of the item, but I'd like to apply that to the first two values of the ListView.
So for anyone interested I solved my problem by parsing the content of the item as a String by doing like this:
String str = (String) listView.getItemAtPosition(position);
String parts[] = str.split(",");
String lat = parts[0];
String lon = parts[1];
I had to get rid of "(" and ")" in the visualization of the coordinates as they were hindering the string parsing.
I'm working on google maps project.Users can add location and view them on the gridview.When I run my app to view all locations from database,all indexes get retrived from database successfuly and pushed arraylist.But when I try to add location.All previous locations get added database again , then my desired location added end of the array.So when I try to reach desired location from gridview,it shows me first location.I want it to be repeated just once.I think there's problem on my select query.Here's output result.
Result
GridView gridView= (GridView) findViewById(R.id.gridView);
list = new ArrayList<>();
try {
MapsActivity.database = this.openOrCreateDatabase("Places",MODE_PRIVATE,null);
Cursor cursor = MapsActivity.database.rawQuery("SELECT * FROM places",null);
int nameIx = cursor.getColumnIndex("name");
int latitudeIx = cursor.getColumnIndex("latitude");
int longitudeIx = cursor.getColumnIndex("longitude");
while (cursor.moveToNext()) {
nameFromDatabase = cursor.getString(nameIx);
String latitudeFromDatabase = cursor.getString(latitudeIx);
String longitudeFromDatabase = cursor.getString(longitudeIx);
image = cursor.getBlob(3);
// names.add(nameFromDatabase);
Double l1 = Double.parseDouble(latitudeFromDatabase);
Double l2 = Double.parseDouble(longitudeFromDatabase);
// System.out.println("coordinates:"+l1+","+l2);
locationFromDatabase = new LatLng(l1, l2);
names.add(nameFromDatabase);
locations.add(locationFromDatabase);
list.add(new Location(nameFromDatabase, image));
}
cursor.close();
} catch (Exception e) {
e.printStackTrace();
}
adapter = new LocationListAdapter(this, R.layout.location_items, list);
gridView.setAdapter(adapter);
I suspect that your issue is that when adding a new location, your get all the locations again from google maps.
Your code doesn't include the code for adding a location.
However, you could prevent duplicated data by defining appropriate UNIQUE constraints for your Places Database.
As an example, based upon your code, you could CREATE the table using :-
CREATE TABLE IF NOT EXISTS places (name TEXT UNIQUE ON CONFLICT IGNORE,longitude REAL,latitude REAL,image BLOB, UNIQUE (longitude,latitude) ON CONFLICT IGNORE)
Using name TEXT UNIQUE ON CONFLICT IGNORE will add a restriction/rule/constraint that the name must be unique and if an attempt is made to use a name that already exists (insert or update) then that attempt will be ignored (no error raised).
Using UNIQUE (longitude,latitude) ON CONFLICT IGNORE does the same BUT for the combination of BOTH columns.
e.g. if a row in the table has latitude 1 and longitude 1 then :-
an attempt to add a row with latitude 1 and longitude 1 will result in the attempt being ignored.
an attempt to add a row with latitude 2 and longitude 1 will work as the combination of the two values has not been used.
likewise, an attempt to add a row with latitude 1 and longitude 2 will work.
i am making a sample App where a Multi markers are on Google Map on Button click i want to store all latitude longitude of each marker in Array List but one marker position is saved on per for loop length can any one tell me how to store a each marker latitude longitude pair in Array?
for (int i=0; i<StopElement.Stop_name_list.size(); i++)
{
position = _Stop_marker.getPosition();
GETLatLng = position.toString();
_getlatlng = GETLatLng.substring(10, GETLatLng.length() - 1);
get_coordinates.add(_getlatlng);
}
I have a web view that shows google maps on my android activity. I am currently getting the link that contains the latitude and longitude values of the selected location by clicking a button. What I want to know is, how can I extract and save the latitude and longitude values in to variables? I tried this code but it doesn't seem to work
Uri uri = Uri.parse(str);
uri.getQueryParameter(q1);
If you have the url as a String, named str:
String[] part1 = str.split(Pattern.quote("#"));
String[] part2 = part1[1].split(Pattern.quote(","));
double lat = Double.parseDouble(part2[0]);
double lon = Double.parseDouble(part2[1]);
I am a begginer in parse, I have been following parse Anywall tutorial for android https://www.parse.com/tutorials/anywall-android
In the example code, there is a customized query, which order items according to the order in which they were created.
// Set up a customized query
ParseQueryAdapter.QueryFactory<AnywallPost> factory =
new ParseQueryAdapter.QueryFactory<AnywallPost>() {
public ParseQuery<AnywallPost> create() {
Location myLoc = (currentLocation == null) ? lastLocation : currentLocation;
ParseQuery<AnywallPost> query = AnywallPost.getQuery();
query.include("user");
query.orderByDescending("createdAt");
query.whereWithinKilometers("location", geoPointFromLocation(myLoc), radius
* METERS_PER_FEET / METERS_PER_KILOMETER);
query.setLimit(MAX_POST_SEARCH_RESULTS);
return query;
}
};
In this partion of the code, I already have the location of the user (myLoc var), and each object retrieved bring its location (under "location" field). My question is, how can I use this fields to order posts according to their distance to the active user?
Distance queries will default to sorting by nearest to farthest, unless you specify some other sort order using orderByAscending()/orderByDescending().
Simply remove any orderBy statements from your query and you'll get the sorting you want.
Amswering myself :)
https://parse.com/docs/android_guide#geo-query
Now that you have a bunch of objects with spatial coordinates, it
would be nice to find out which objects are closest to a point. This
can be done by adding another restriction to ParseQuery using
whereNear.
ParseQuery<ParseObject> query = ParseQuery.getQuery("PlaceObject");
query.whereNear("location", userLocation);