dynamically populating maps in android - android

So I have a mapView and I want to dynamically populate it based on a database. I can do each geopoint indidivually but for some reason when i try to do them all it doesn't display the points on the map.. If anyone has suggestions please let me know :-)
public class FieldTrip extends MapActivity {
private MapView map=null;
private MyLocationOverlay me=null;
private String theLat;
private String theLong;
private Double theDLat;
private Double theDLong;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.maptastic);
map=(MapView)findViewById(R.id.mapView);
map.getController().setCenter(getPoint(40.76793169992044,
-73.98180484771729));
map.getController().setZoom(17);
map.setBuiltInZoomControls(true);
Drawable marker=getResources().getDrawable(R.drawable.supaaaa);
marker.setBounds(0, 0, marker.getIntrinsicWidth(),
marker.getIntrinsicHeight());
map.getOverlays().add(new SitesOverlay(marker));
me=new MyLocationOverlay(this, map);
map.getOverlays().add(me);
}
#Override
public void onResume() {
super.onResume();
me.enableCompass();
}
#Override
public void onPause() {
super.onPause();
me.disableCompass();
}
#Override
protected boolean isRouteDisplayed() {
return(false);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_S) {
map.setSatellite(!map.isSatellite());
return(true);
}
else if (keyCode == KeyEvent.KEYCODE_Z) {
map.displayZoomControls(true);
return(true);
}
return(super.onKeyDown(keyCode, event));
}
private GeoPoint getPoint(double lat, double lon) {
return(new GeoPoint((int)(lat*1000000.0),
(int)(lon*1000000.0)));
}
private class SitesOverlay extends ItemizedOverlay<OverlayItem> {
private List<OverlayItem> items=new ArrayList<OverlayItem>();
///need to make this dy-NAMIK :p
public SitesOverlay(Drawable marker) {
super(marker);
for (Entry<Integer, FieldTripStop> cur : Statics.fieldTripStops.entrySet())
{
// get the FieldTripStop object from the current hash table entry
Statics.currentFTStop = cur.getValue();
// concatenate numbers before (all) and after (6) the decimal, since
// geopoints only accept 6 numbers past the decimal.
theLat = Statics.currentFTStop.latitude;
theLong = Statics.currentFTStop.longitude;
theDLat = Double.parseDouble(theLat);
theDLong = Double.parseDouble(theLong);
boundCenterBottom(marker);
items.add(new OverlayItem(getPoint(theDLat,
theDLong),
"RANDOM TEXT?",
"RANDOM TEXT LALALA"));
}
populate();
}
#Override
protected OverlayItem createItem(int i) {
return(items.get(i));
}
#Override
protected boolean onTap(int i) {
Toast.makeText(FieldTrip.this,
items.get(i).getSnippet(),
Toast.LENGTH_SHORT).show();
return(true);
}
#Override
public int size() {
return(items.size());
}
}
}

This code looks correct to me. If you are not seeing all of the points, then fieldTripStops doesn't have all of the points you wish to display. Double check the elements in the fieldTripStops set.

Related

Issue while using 2 map fragments in One application

I am trying to use two different Map Fragment in two different Fragment Activities. And as per the SupportMapFragment design, I am keeping the map key in the META TAG of the appilcation properties in Manifest file.
However my Map view is not clearing, it is showing same map with old points in the second Fragment eventhough I am calling "getMap().clear()". Please suggest
This is working fine untill and unless I dont use new MapFragment in a separate Activity, If I use new Mapfragment, it is taking the same map view, so the map view is not clearing.
Code :
MAP FRAGMENT
public class DisplayMapFragment extends SupportMapFragment implements
OnMarkerClickListener,OnInfoWindowClickListener {
View mMapViewContainer;
MapView mMapView;
private Utill utilClass = Utill.getInstance(false);
private View v;
private MapView mapView;
private GeoPoint p;
private MapController mc;
private String latstr = "";
private String lonstr = "";
private String name, address;
private double lat;
private double lng;
ArrayList<NearByItem> NearByList=new ArrayList<NearByItem>();
private LatLng mPosFija;
private ArrayList<Double> mlat, mlong;
private ViewGroup parent;
Utill utilclass=Utill.getInstance(false);
Bitmap bmp;
ImageView ivIcon;
StringBuilder sbc=new StringBuilder();
ProgressBar progressbar;
public ImageLoader imageLoader;
public DisplayMapFragment() {
super();
}
public static DisplayMapFragment newInstance(LatLng posicion) {
DisplayMapFragment frag = new DisplayMapFragment();
frag.mPosFija = posicion;
return frag;
}
#Override
public void onCreate(Bundle arg0) {
// TODO Auto-generated method stub
super.onCreate(arg0);
NearByList=NearByMapActivity.fltrdNearByLists;
imageLoader=new ImageLoader(this.getActivity().getApplicationContext());
}
#Override
public View onCreateView(LayoutInflater arg0, ViewGroup arg1, Bundle arg2) {
View v = super.onCreateView(arg0, arg1, arg2);
mlat = new ArrayList<Double>();
mlong = new ArrayList<Double>();
parent=arg1;
if(NearByMapActivity.fltrdNearByLists!=null && !NearByMapActivity.fltrdNearByLists.isEmpty())
NearByList.addAll(NearByMapActivity.fltrdNearByLists);
else
NearByList.addAll(Utill.NearbyList);
NearByMapActivity parentActivity = (NearByMapActivity) getActivity();
return v;
}
public class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private List<OverlayItem> items;
private Drawable marker;
public MyItemizedOverlay(Drawable defaultMarker) {
super(defaultMarker);
items = new ArrayList<OverlayItem>();
marker = defaultMarker;
}
#Override
protected OverlayItem createItem(int index) {
return items.get(index);
}
#Override
public int size() {
return items.size();
}
#Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
super.draw(canvas, mapView, shadow);
boundCenterBottom(marker);
}
public void addItem(OverlayItem item) {
items.add(item);
}
public void populateNow() {
populate();
}
#Override
protected boolean onTap(int index) {
return true;
}
}
private void initMap() {
UiSettings settings = getMap().getUiSettings();
settings.setAllGesturesEnabled(true);
// settings.setMyLocationButtonEnabled(true);
settings.isZoomControlsEnabled();
getMap().setTrafficEnabled(true);
getMap().getMaxZoomLevel();
// getMap().setMyLocationEnabled(true);
if(NearByMapActivity.fltrdNearByLists!=null && !NearByMapActivity.fltrdNearByLists.isEmpty())
getMap().moveCamera(
CameraUpdateFactory.newLatLngZoom(
new LatLng(Double.parseDouble(NearByMapActivity.fltrdNearByLists.get(0).getLocationLat()),
Double.parseDouble(NearByMapActivity.fltrdNearByLists.get(0).getLocationLong())), 14));
getMap().isTrafficEnabled();
// getMap().addGroundOverlay(new GroundOverlayOptions().)
getMap().setOnMarkerClickListener(this);
getMap().setOnInfoWindowClickListener(this);
marker=new MarkerOptions()
.position(
new LatLng(
Double.parseDouble(item.getLocationLat()),
Double.parseDouble(item.getLocationLong())))
.title(""+item.getStoreTitle())
.icon(bitmapLoyalty
).snippet(sbc.toString());
else
marker=new MarkerOptions()
.position(
new LatLng(
Double.parseDouble(item.getLocationLat()),
Double.parseDouble(item.getLocationLong())))
.title(""+item.getStoreTitle())
.icon(bitmapIconNearBy
).snippet(sbc.toString());
Marker marker1=getMap().addMarker(marker);
PopupWindowAdapter view1=new PopupWindowAdapter();
this.getMap().setInfoWindowAdapter(view1);
// .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
bmp=BitmapFactory.decodeResource(getResources(),R.drawable.orgr_icon);
}
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
initMap();
getMap().addMarker(new MarkerOptions().position(new
LatLng(36.865814,-119.758399)).icon(BitmapDescriptorFactory.fromResource(R.drawable.red_pin)));
//getMap().addGroundOverlay(new GroundOverlay(null));
/* getMap().addGroundOverlay(new GroundOverlayOptions()
.image(image)
.positionFromBounds(bounds)
.transparency(0.5));
*/ }
#Override
public void onPause() {
super.onPause();
//getMap().clear();
}
public void onResume(){
super.onResume();
if(NearByMapActivity.mainMapView!=null)
//if(NearByMapActivity.mainMapView.getVisibility()==View.GONE)
NearByMapActivity.mainMapView.setVisibility(View.VISIBLE);
}
#Override
public boolean onMarkerClick(Marker arg0) {
// TODO Auto-generated method stub
Log.e("cleared", "");
return false;
}
}
public void onStop(){
super.onStop();
//getMap().clear();
}
}
MAP ACTIVITY : Activity class to start Fragment.
public class NearByMapActivity extends FragmentActivity{
onCreate(){
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
Fragment frag=new DisplayMapFragment();
transaction.add(R.id.middle_view, frag);
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
transaction.addToBackStack(null);
transaction.commit();
}
}
You are probably affected by this bug:
https://code.google.com/p/gmaps-api-issues/issues/detail?id=5027&thanks=5027&ts=1362071369
Thank u so much!!!!!!!!!!!!!
I try this, and its work like a charm :)
public void hideStupidMaps() {
mMapView.getLayoutParams().height = 1;
mMapView.getLayoutParams().width = 1;
mMapView.invalidate();
mMapView.requestLayout();
}

ItemizedOverlay OnTap() not firing

I've defined a map overlay, and I can display markers without issue. I'm now trying to get something to happen when I tap one, but the event never seems to fire. I'm sure I'm missing something obvious...
public class MapBlobCollection extends ItemizedOverlay<OverlayItem> {
#SuppressWarnings("serial")
public class ItemTappedEvent extends EventObject
{
public ItemTappedEvent(int itemIndex) {
super(itemIndex);
}
}
private ArrayList<OverlayItem> myOverlays ;
public MapBlobCollection(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
myOverlays = new ArrayList<OverlayItem>();
populate();
}
public void addOverlay(OverlayItem overlay){
myOverlays.add(overlay);
populate();
}
#Override
protected OverlayItem createItem(int i) {
return myOverlays.get(i);
}
// Removes overlay item i
public void removeItem(int i){
myOverlays.remove(i);
populate();
}
// Returns present number of items in list
#Override
public int size() {
return myOverlays.size();
}
public void addOverlayItem(OverlayItem overlayItem) {
myOverlays.add(overlayItem);
populate();
}
public void addOverlayItem(int lat, int lon, String title) {
try {
GeoPoint point = new GeoPoint(lat, lon);
OverlayItem overlayItem = new OverlayItem(point, title, null);
addOverlayItem(overlayItem);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
#Override
protected boolean onTap(int index) {
super.onTap(index);
Log.d("TESTING","Triggering tap event on " + Integer.toString(index));
EventManager.triggerEvent(this, new ItemTappedEvent(index));
return true;
}
}
Basically, the debug log entry isn't written and the event doesn't fire.
In addition, my mapview itself doesn't pan around (should it, without any extra code from me?) and despite setting the setBuitInZoomControls(true), these don't appear either... so perhaps the mapview itself is at fault?
The mapview is defined in the layout as:
<com.google.android.maps.MapView
android:id="#+id/indexMapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:apiKey="#string/mapskey_release"/>
And I'm not overriding any draw events or anything...
I believe you need to add
android:clickable="true"
to your mapview
Try moving the super function to the end:
#Override
protected boolean onTap(int index) {
Log.d("TESTING","Triggering tap event on " + Integer.toString(index));
EventManager.triggerEvent(this, new ItemTappedEvent(index));
return super.onTap(index);
}

How to open from a marker/pin in MapView a new Activity/Layout?

How can I open from a Pin/Marker a new Activity in Android MapView?
Like this, and then I´d like to tap on a marker and it should open a new activity,
how can I do this?
MyCode:
private GeoPoint getPoint(double lat, double lon) {
return(new GeoPoint((int)(lat*1000000.0),
(int)(lon*1000000.0)));
}
private class SitesOverlay extends ItemizedOverlay {
private List items=new ArrayList();
public SitesOverlay(Drawable marker) {
super(marker);
boundCenterBottom(marker);
items.add(new OverlayItem(getPoint(47.17612,
8.499727),
"TCS", "TCS Camping Brüggli"));
items.add(new OverlayItem(getPoint(47.57612,
8.499727),
"",
""));
items.add(new OverlayItem(getPoint(00.765136435316755,
00.97989511489868),
"",
""));
items.add(new OverlayItem(getPoint(00.0686417491799,
00.01572942733765),
"",
""));
populate();
}
#Override
protected OverlayItem createItem(int i) {
return(items.get(i));
}
#Override
protected final boolean onTap(int i) {
//Do you job here.
startActivity(new Intent(MaptestActivity.this,SettingsActivity.class));
return false;
https://github.com/jgilfelt/android-mapviewballoons
In this link we have one package " mapviewballoons.example.simple "
In this " mapviewballoons.example.simple " package has one class " SimpleItemizedOverlay.java " some modification in this code.
package mapviewballoons.example.simple;
public class SimpleItemizedOverlay extends BalloonItemizedOverlay {
private ArrayList<OverlayItem> m_overlays = new ArrayList<OverlayItem>();
private Context c;
public SimpleItemizedOverlay(Drawable defaultMarker, MapView mapView) {
super(boundCenter(defaultMarker), mapView);
c = mapView.getContext();
}
public void addOverlay(OverlayItem overlay) {
m_overlays.add(overlay);
populate();
}
#Override
protected OverlayItem createItem(int i) {
return m_overlays.get(i);
}
#Override
public int size() {
return m_overlays.size();
}
#Override
protected boolean onBalloonTap(int index, OverlayItem item) {
Toast.makeText(c, "onBalloonTap for overlay index " + index,
Toast.LENGTH_LONG).show();
c.startActivity(new Intent(c.getApplicationContext(), NewActivity.class));
return false;
}
}
This is modification code ,
#Override
protected boolean onBalloonTap(int index, OverlayItem item) {
Toast.makeText(c, "onBalloonTap for overlay index " + index,
Toast.LENGTH_LONG).show();
c.startActivity(new Intent(c.getApplicationContext(), NewActivity.class));
return false;
}
Call different activities,
#Override
protected boolean onBalloonTap(int index, OverlayItem item) {
Toast.makeText(c, "onBalloonTap for overlay index " + index,
Toast.LENGTH_LONG).show();
if (index == 0)
{
c.startActivity(new Intent(c.getApplicationContext(), NewActivity.class));
} else {
c.startActivity(new Intent(c.getApplicationContext(), secondNewActivity.class));
return false;
}

Programmatically adding and removing markers

I've been following the NooYawk MapView example with reasonable success. I've replaced the hardcoded geopoints and descriptions based on some system update messages. New markers are added just fine when a new update occurs. The problem is the markers don't go away when the problem is resolved.
I'd be happy enough to remove all markers on press of the refresh button as they get added back in.
Any ideas?
The below is somewhat sanitized.
private class SitesOverlay extends ItemizedOverlay<OverlayItem> {
private List<OverlayItem> items=new ArrayList<OverlayItem>();
private Drawable marker=null;
public SitesOverlay(Drawable marker) {
super(marker);
this.marker=marker;
try {
data = getData();
} catch (MalformedURLException e) {
//
}
if (!data.equals("")) {
// process data
for (Integer i = 0; i < outages.length; i++) {
items.add(new OverlayItem(
getPoint(lat, lng, headerMsg, bodyMsg));
}
}
populate();
}
#Override
protected OverlayItem createItem(int i) {
return(items.get(i));
}
#Override
public void draw(Canvas canvas, MapView mapView,
boolean shadow) {
super.draw(canvas, mapView, false);
boundCenterBottom(marker);
}
#Override
protected boolean onTap(int i) {
Toast.makeText(getBaseContext(),
items.get(i).getSnippet(),
Toast.LENGTH_LONG).show();
return(true);
}
#Override
public int size() {
return(items.size());
}
}
on Refresh Button click mapview.getOverlays().clear(); Then start push pin in map and after adding Overlays you should mapview.postinvalidate()

OverlayItem not displaying markers!

I have the following code and the markers are not appearing on the map at all!
private class SitesOverlay extends ItemizedOverlay<pfOverlayItem> {
private List<pfOverlayItem> items=new ArrayList<pfOverlayItem>();
//private PopupPanel panel=new PopupPanel(R.layout.popup);
public SitesOverlay() {
super(null);
items = mainOverlayArray;
populate();
}
#Override
protected pfOverlayItem createItem(int i) {
return(items.get(i));
}
#Override
public void draw(Canvas canvas, MapView mapView,
boolean shadow) {
super.draw(canvas, mapView, shadow);
}
#Override
public int size() {
return(items.size());
}
private Drawable getMarker(int resource) {
Drawable marker=getResources().getDrawable(resource);
marker.setBounds(0, 0, marker.getIntrinsicWidth(),
marker.getIntrinsicHeight());
boundCenter(marker);
return(marker);
}
}
mainOverlayArray is full of pfOverlayItem's and the code for that class is
public class pfOverlayItem extends OverlayItem {
private String coolText;
public String getcoolText() {
return coolText;
}
public void setcoolText(String coolText) {
this.coolText = coolText;
}
public pfOverlayItem(GeoPoint point, String title, String snippet) {
super(point, title, snippet);
// TODO Auto-generated constructor stub
}
}
I also set the marker outside of this after processing an XML file...
ArrayList<pfOverlayItem> overArray = myXMLHandler.getOverlayArray();
mainOverlayArray = overArray;
pfOverlayItem tempOver = null;
Drawable marker = getResources().getDrawable(R.drawable.icon);
for (int i = 0; i < mainOverlayArray.size(); i++) {
tempOver = mainOverlayArray.get(i);
tempOver.setMarker(marker);
}
sites=new SitesOverlay();
myMapView.getOverlays().add(sites);
myMapView.invalidate(); [/code]
It looks as though you're starting from one of my many sample Google Map applications. Your code as shown here is incomplete (e.g., according to the code here, you never create any OverlayItem instances).
My recommendation is you roll back to one of the samples I link to above and start modifying from there, or you start trying to figure out which of your methods are getting called and which are not.

Categories

Resources