Is it possible to make the addition of a geopoint in a map more "automatic"? I mean, if we have many points to add in the map (more than 100), we don't have to add them one by one like that:
GeoPoint point2 = new GeoPoint(microdegrees(36.86774),microdegrees(10.305302));
GeoPoint point3 = new GeoPoint(microdegrees(36.87154),microdegrees(10.341815));
GeoPoint point4 = new GeoPoint(microdegrees(36.876093),microdegrees(10.325716));
pinOverlay.addPoint(point2);
pinOverlay.addPoint(point3);
pinOverlay.addPoint(point4);
Is there a method to stock them all in a table and then the compiler adds them one by one?
You'll want to store them in either a SQLite database or some other form of data storage, and then you can pull them in and place them on the map with an ItemizedOverlay, see Google Map View (a tutorial).
Create your array from a database cursor
Cursor cursor = mDbHelper.getItems();
cursor.moveToFirst();
List<CatchItem> catchList = new ArrayList<CatchItem>();
if (cursor != null && cursor.getCount() > 0) {
for (int i = 0; i < cursor.getCount(); i++) {
CatchItem item = new CatchItem();
item.Latitude = cursor.getDouble(cursor.getColumnIndex("latitude"));
item.Longitude = cursor.getDouble(cursor.getColumnIndex("longitude"));
catchList.add(item);
cursor.moveToNext();
}
}
ItemizedOverlay
List<Overlay> overlays = mMaps.getOverlays();
overlays.clear();
CatchesItemizedOverlay catchOverlays = new CatchesItemizedOverlay(getResources().getDrawable(R.drawable.map_markeroverlay_blue72), this);
for (int i = 0; i < catchList.size(); i++) {
double lat = catchList.get(i).Latitude;
double lng = catchList.get(i).Longitude;
GeoPoint geopoint = new GeoPoint((int)(lat*1E6), (int)(lng*1E6));
catchOverlays.addOverlay(new CatchOverlayItem(this, geopoint, catchList.get(i)));
}
overlays.add(catchOverlays);
CatchesItemizedOverlay is my own extended ItemizedOverlay (I needed custom functionality). The catchList object is just a custom object that has latitude and longitude.
Hopefully this works for you.
You could store them in a SQLite databse, and pull them as needed
Related
I have a huge list in an XML tag like so:
<coor> -123.3858,41.34119,0
-123.3856,41.34109,0
-123.3852,41.34121,0
-123.3848,41.34139,0</coor>
and need it like this:
new LatLng(-123.3858,41.34119),
new LatLng(-123.3856,41.34109),
new LatLng(-123.3852,41.34121),
new LatLng(-123.3848,41.34139),
to work with google maps v2 android.
I've done a string replace on the coordinates and am getting the correct results like so:
String ll = "),new LatLng(";
coor = coor.replaceAll(",0", ll);
replacing the ,0 for the new LatLng(... I am not figuring out how to change the large string of latlng text into latlng locations to put into my polygon:
PolygonOptions perimeteres = new PolygonOptions().add(coor);
Is there way to do this? Or do I need to separate each out and make them individual latlng?
EDIT::::
String[] splitData = coor.split(",0");
for (String eachSplit : splitData) {
if (!eachSplit.endsWith(",0")) {
//Log.e("EACH",eachSplit);
Log.v("e","new LatLon("+eachSplit+");");
}
}
This is getting me a little closer...
You are going completely in the wrong direction, this
String ll = "),new LatLng(";
coor = coor.replaceAll(",0", ll);
is not the same as
new LatLng(-123.3858,41.34119)
the first gives you a string which does nothing for you, the second is an object which is what you need.
Edit
you need to remove the 0 from the coordinates then you do a string split on the , so you have an array of latitudes and longitudes.
then create a List<LatLng> which is what you need to create a polygon of points
and loop through your points
for(int j=0;j<locationAry.length;j++){
if(j%2 == 0){
lon = Float.parseFloat(locationAry[j+1]);
lat = Float.parseFloat(locationAry[j]);
}
}
i am currently trying to implement an ActionBar-Button that on usage sets all my markers on my GoogleMap-object visible or invisible. My problem is that i don't know how i can get a reference to all my markers once they have been created and are shown on my map. Im looking for a solution where i stash all my marker-objects into an array, that i can access in other parts of my code aswell. is this approach reasonable?
here is what i am thinking of:
private Marker[] mMarkerArray = null;
for (int i = 0; i < MainActivity.customers.size(); i++) {
LatLng location = new LatLng(mData.lat, mData.lng);
Marker marker = mMap.addMarker(new MarkerOptions().position(location)
.title(mData.title)
.snippet(mData.snippet));
mMarkerArray.add(marker);
}
and set all my markers invisible on within another method:
for (int i = 0; i < mMarkerArray.length;; i++) {
mMarkerArray[i].setVisible(false);
}
it refuses to add the markers to a Marker[]-array. how can i achieve it?
mMarkerArray.add(marker) doesnt work
i figured out an answer that also regards my customerList having customers without coordinates --> (0,0;0,0). inspired by this blog.
initialize ArrayList:
private ArrayList<Marker> mMarkerArray = new ArrayList<Marker>();
add marker to my map and to the mMarkerArray:
for (int i = 0; i < MainActivity.customers.size(); i++) {
Customer customer = MainActivity.customers.get(i);
if (customer.getLon() != 0.0) {
if (!customer.isProspect()) {
Data mData= new Data(customer.getLat(),customer.getLon(),customer.getName(),
customer.getOrt());
LatLng location = new LatLng(mData.lat, mData.lng);
Marker marker = mMap.addMarker(new MarkerOptions().position(location)
.title(mData.title)
.snippet(mData.snippet));
mMarkerArray.add(marker);
}}}
set all markers not-visible
for (Marker marker : mMarkerArray) {
marker.setVisible(false);
//marker.remove(); <-- works too!
}
Replace
private Marker[] mMarkerArray = null;
with
private List<Marker> mMarkerArray = new ArrayList<Marker>();
and you should be fine.
If you use Android Maps Extensions, you can simply iterate over all markers using
for (Marker marker : googleMap.getMarkers()) {
marker.setVisible(false);
}
without having your own List of all Markers.
You can keep a Collection of OverlayItem within your Activity or Fragment and then call MapView.getOverlays().clear() to make them "invisible" and then add them back to make them visible again. Call MapView.invalidate() after each action to cause the map to be repainted.
I have 4 rows of latitude in database and I want to get all that rows of it. In CafeDataSource I had query it and set it to ArrayList<HashMap<String, Object>>.
When I use it in TopActivity in forloop, I can query all 4 rows. but all 4 rows just have one value of the last row. Ex, my database (1,2,3,4) but my result (4,4,4,4).
I had log(db) in getArrCursor() for value and it works fine.
I had log(number) in for loop for value but it shows me all and all are the last row.
How can I query it all and different row?
CafeDataSource
public ArrayList<HashMap<String, Object>> getArrCursor(){
arrCursor = new ArrayList<HashMap<String,Object>>();
HashMap<String, Object> map;
Cursor cursor = database.rawQuery("SELECT * FROM "+CafeDbOpenHelper.TABLE_CAFE, null);
if(cursor != null){
while(cursor.moveToNext()){
map = new HashMap<String, Object>();
map.put(CafeDbOpenHelper.CAFE_TITLE, cursor.getString(cursor.getColumnIndex(CafeDbOpenHelper.CAFE_TITLE)));
map.put(CafeDbOpenHelper.CAFE_THUMB, cursor.getString(cursor.getColumnIndex(CafeDbOpenHelper.CAFE_THUMB)));
map.put(CafeDbOpenHelper.CAFE_LATITUDE, cursor.getDouble(cursor.getColumnIndex(CafeDbOpenHelper.CAFE_LATITUDE)));
map.put(CafeDbOpenHelper.CAFE_LONGITUDE, cursor.getDouble(cursor.getColumnIndex(CafeDbOpenHelper.CAFE_LONGITUDE)));
Log.i("db", "" +cursor.getDouble(cursor.getColumnIndex(CafeDbOpenHelper.CAFE_LATITUDE)));
arrCursor.add(map);
}
}
return arrCursor;
}
TopActivity
int position = arrCursor.size();
for (int i = 0; i < position; i++) {
Log.i("number", "" +arrCursor.get(position -1).get(CafeDbOpenHelper.CAFE_LATITUDE));
double lat = (Double) arrCursor.get(position -1).get(CafeDbOpenHelper.CAFE_LATITUDE);
double lng = (Double) arrCursor.get(position -1).get(CafeDbOpenHelper.CAFE_LONGITUDE);
LatLng latlong = new LatLng(lat, lng);
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
Marker maker = map.addMarker(new MarkerOptions().position(latlong).title((String) arrCursor.get(position -1).get(CafeDbOpenHelper.CAFE_TITLE)));
// Move the camera instantly to hamburg with a zoom of 15.
map.moveCamera(CameraUpdateFactory.newLatLngZoom(latlong, 17));
LinearLayout includeMap = (LinearLayout) findViewById(R.id.lin_map);
includeMap.setVisibility(v.VISIBLE);
}
Position never changes but you are using that to get from the Array every time. Try instead
double lat = (Double) arrCursor.get(i).get(CafeDbOpenHelper.CAFE_LATITUDE);
double lng = (Double) arrCursor.get(i).get(CafeDbOpenHelper.CAFE_LONGITUDE);
Edit
Did you change this line
Marker maker = map.addMarker(new MarkerOptions().position(latlong).title((String) arrCursor.get(position -1).get(CafeDbOpenHelper.CAFE_TITLE)));
to
Marker maker = map.addMarker(new MarkerOptions().position(latlong).title((String)
arrCursor.get(i).get(CafeDbOpenHelper.CAFE_TITLE)));
You are starting at the i position of your Array so every time you want to access that postion you will use i
My Problem is I have 4 Location's Latitude and Longitude into my local database, I am fetching those data and use for draw route path in android but problem is first to second location route is not display on mapview other second to third and third to fourth route path is display on mapview. Sorry for bad English communication.
i am getting code from following link for draw route path.
MapRoute Example
and call drawing class using following function:-
public void drawpath(){
mDb.open();
Cursor cr = mDb.getAllTitles();
cr.moveToFirst();
if (cr.getCount() > 0) {
for (int i = 0; i <= cr.getCount()/2; i++) {
fromlat = Double.parseDouble(cr.getString(1));
fromlng = Double.parseDouble(cr.getString(2));
cr.moveToNext();
tolat = Double.parseDouble(cr.getString(1));
tolng = Double.parseDouble(cr.getString(2));
String url = RoadProvider
.getUrl(fromlat, fromlng, tolat, tolng);
InputStream is = getConnection(url);
mRoad = RoadProvider.getRoute(is);
MapOverlay mapOverlay = new MapOverlay(mRoad, mapView);
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.add(mapOverlay);
mapView.invalidate();
}
}
cr.close();
mDb.close();
}
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("http://maps.google.com/maps?saddr=20.344,34.34&daddr=20.5666,45.345"));
startActivity(intent);
It is happening because you are using cr.getCount()/2.
When you will have even number of count then it will work not work fine
For example if you have 4 number of rows then you cr.getCount()/2 = 2
so your loop will continue from 0 to 2 means 3 times.
Actually it should be continue 2 times as your coding.
Now lat take a odd numbers say 5 so cr.getCount()/2 = 2 because i is the integer value so your loop will work total 3 times.
So you have different mechanism.
Like try to add all the lat and long in the arraylist and then after loop complete.
Make a loop of the size of that arrayList and create a path.May be for that you required two additional variable to store the previous lat and long.
Firstly I add markers to the overlay:
private MapOverlay itemizedOverlay;
Cursor items = mDbHelper.fetchAllItems();
startManagingCursor(items);
for (int i = 0; i < items.getCount(); i++) {
items.moveToPosition(i);
OverlayItem overlayItem = new OverlayItem(markerPoint, "", "");
itemizedOverlay.addOverlay(overlayItem);
}
mapOverlays.add(itemizedOverlay);
Now I need to update markers (change drawable).
Can I do:
Cursor items = mDbHelper.fetchAllItems();
startManagingCursor(items);
for (int i = 0; i < items.getCount(); i++) {
items.moveToPosition(i);
itemizedOverlay.getItem(i).setMarker();
}
mapOverlays.add(itemizedOverlay);
Will itemizedOverlay.getItem(i) always return items in the same sequence?
Records in the database are not added/deleted.
Will itemizedOverlay.getItem(i) always return items in the same sequence?
That is up to you. You are the one implementing getItem().