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();
}
Related
I had this activity:
public class MapViewer extends Activity {
private GoogleMap map;
private Database db = new Database(this);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mapviewer);
try {
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
if (map != null) {
map.setMyLocationEnabled(true);
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
map.getUiSettings().setRotateGesturesEnabled(false);
this.addMerchantMarkers(new MarkerOptions());
}
} catch (NullPointerException e) {
e.printStackTrace();
}
}
public void addMerchantMarkers(MarkerOptions mo) {
SQLiteDatabase dbRead = db.getReadableDatabase();
Cursor result = dbRead.rawQuery("SELECT title, addr, lat, lon FROM users", null);
while(result.moveToNext()) {
map.addMarker(mo.position(new LatLng(result.getFloat(2), result.getFloat(3)))
.title(result.getString(0))
.snippet(result.getString(1))
);;
}
}
}
that i changed in the following way to use markers clustering:
public class MapViewer extends Activity {
private GoogleMap map;
private Database db = new Database(this);
private ClusterManager<MyItem> mClusterManager;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mapviewer);
try {
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
if (map != null) {
map.setMyLocationEnabled(true);
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
map.getUiSettings().setRotateGesturesEnabled(false);
setUpClusterer();
}
} catch (NullPointerException e) {
e.printStackTrace();
}
}
private void setUpClusterer() {
mClusterManager = new ClusterManager<MyItem>(this, map);
map.setOnCameraChangeListener(mClusterManager);
map.setOnMarkerClickListener(mClusterManager);
addItems();
}
private void addItems() {
SQLiteDatabase dbRead = db.getReadableDatabase();
Cursor result = dbRead.rawQuery("SELECT lat, lon, title, addr FROM users", null);
while(result.moveToNext()) {
MyItem offsetItem = new MyItem(result.getFloat(0), result.getFloat(1));
mClusterManager.addItem(offsetItem);
}
}
}
Now i don't know how to add title, snippet and icon to every marker like previous code.
Now if i click on single marker nothing happens...
How to get that?
Here you should work with ClusterManager itself.
For example, setting on cluster item click:
mClusterManager.setOnClusterItemClickListener(new ClusterManager.OnClusterItemClickListener<MyItem>() {
#Override
public boolean onClusterItemClick(MyItem item) {
//put your code here
return false;
}
});
And there are other different methods in ClusterManager class.
You'll need a less obvious code to assign info window to marker or cluster - you should use this code:
mClusterManager.getMarkerCollection().setOnInfoWindowAdapter(new MarkerInfoWindowAdapter());
mClusterManager.getClusterMarkerCollection().setOnInfoWindowAdapter(new ClusterInfoWindow());
There MarkerInfoWindowAdapter and ClusterWindowAdapter is your classes, which implements GoogleMap.InfoWindowAdapter.
With icons it's a little harder, because clustering changes icons to default. You should use method:
public void setRenderer(com.google.maps.android.clustering.view.ClusterRenderer<T> view)
You could your own renderer class and override some methods. For example, to set custom icons use such a class:
class OwnIconRendered extends DefaultClusterRenderer<MyItem> {
public OwnIconRendered(Context context, GoogleMap map,
ClusterManager<MyItem> clusterManager) {
super(context, map, clusterManager);
}
#Override
protected void onBeforeClusterItemRendered(MyItem item, MarkerOptions markerOptions) {
markerOptions.icon(item.getIcon());
markerOptions.snippet(item.getSnippet());
markerOptions.title(item.getTitle());
super.onBeforeClusterItemRendered(item, markerOptions);
}
}
And use it in such way:
mClusterManager.setRenderer(new OwnIconRendered(activity.getApplicationContext(), getMap(), mClusterManager));
I have an extended overlay class:
short code:
public class MapOverlay extends Overlay {
private Context context;
private ProgressDialog dDialog;
Drawable drawable;
GeoPoint MainPoint;
MapView mapView;
public MapOverlay(Context context, MapView mapView)
{
this.context = context;
this.mapView = mapView;
}
#Override
public boolean onTap(GeoPoint p, MapView mapView)
{
this.MainPoint = p;
AlertDialog.Builder dialog = new AlertDialog.Builder(context);
dialog.setMessage("Do you want to set point here?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
setPoint();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
dialog.show();
return true;
}
public void setPoint()
{
OverlayItem overlayitem = new OverlayItem(MainPoint, "Hi!", "You touched this location!");
}
I want to draw the touched point on my mapview, which is in this activity:
public class MyMapLocationActivity extends MapActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
drawable = this.getResources().getDrawable(R.drawable.androidmarker);
MapOverlay myOverlay = new MapOverlay(this, mapView);
mapView.getOverlays().add(myOverlay);
mapView.postInvalidate();
}
I want to mark the touched point, after confirming the dialog box from MapOverlay class. I think I'm missing to pass something - what more should I do?
You need to have a class which extends ItemizedOverlay, which in turn can hold an OverlayItem, which takes a GeoPoint in its constructor. Something like
public class MapDemo extends MapActivity implements OnTouchListener {
private Drawable mDrawable;
private ItemizedMapOverlay mItemizedOverlay;
private OverlayItem mOverlayitem;
private GeoPoint mClickedPoint = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GeoPoint mapCentrePoint = new GeoPoint(51500000, 0);
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setOnTouchListener(this);
MapController mapCtrlr = mapView.getController();
mapView.setBuiltInZoomControls(true);
mapCtrlr.setZoom(8);
mapCtrlr.setCenter(mapCentrePoint);
List<Overlay> mapOverlays = mapView.getOverlays();
mDrawable = this.getResources().getDrawable(R.drawable.icon);
mItemizedOverlay = new ItemizedMapOverlay(mDrawable, this);
mapOverlays.add(mItemizedOverlay);
}
#Override
protected boolean isRouteDisplayed() {return false;}
#Override
public boolean onTouch(View v, MotionEvent e) {
if (e.getAction() == MotionEvent.ACTION_DOWN) {
final View fv = v;
AlertDialog.Builder dialog = new AlertDialog.Builder(v
.getRootView().getContext());
dialog.setMessage("Do you want to set point here?")
.setCancelable(false)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
setPoint(fv);
}
})
.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
dialog.cancel();
}
});
dialog.show();
// Can't show point till +ve button selected, so store it
mClickedPoint = ((MapView) v).getProjection()
.fromPixels((int)e.getX(), (int)e.getY());
}
return true;
}
void setPoint(View v) {
if (mClickedPoint != null) {
mOverlayitem = new OverlayItem(mClickedPoint, "test", "test2");
mOverlayitem.setMarker(mDrawable);
mItemizedOverlay.clear(); // clear last marker
mItemizedOverlay.addOverlay(mOverlayitem);
v.postInvalidate();
}
}
}
and
public class ItemizedMapOverlay extends ItemizedOverlay {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
public ItemizedMapOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
public ItemizedMapOverlay(Drawable defaultMarker, Context context) {
super(boundCenterBottom(defaultMarker));
}
#Override
protected OverlayItem createItem(int i) {return mOverlays.get(i);}
#Override
public void draw(android.graphics.Canvas canvas, MapView mapView,
boolean shadow) {
super.draw(canvas, mapView, shadow);
}
#Override
public int size() { return mOverlays.size();}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
public void clear() {
mOverlays.clear();
}
}
should be close enough for you to adapt.
I am currently doing a MapView with mapquest and require adding overlays to locations. Currently my MapView is working however the overlay does not appear. Any ideas as to how it can be solved? Also is it possible to create an onClick on the overlay to bring me to a ListView?? Thanks is advance!! my main activity
public class QMapsActivity extends MapActivity {
protected MapView map;
AnnotationView annot;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutId());
init();
annot = new AnnotationView(map);
}
protected void init() {
// TODO Auto-generated method stub
this.setupMapView();
}
protected void setupMapView() {
// set the zoom level, center point and enable the default zoom controls
map = (MapView) findViewById(R.id.map);
map.getController().setZoom(16);
map.getController().setCenter(new GeoPoint(1.309503,103.777793));
map.setBuiltInZoomControls(true);
}
protected int getLayoutId() {
return R.layout.main;
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
And my Overlay class
public class QPoiOverlay extends QMapsActivity{
protected void init() {
super.init();
setupMapView();
}
protected void addPoiOverlay() {
// TODO Auto-generated method stub
Drawable icon = getResources().getDrawable(R.drawable.location_marker);
final DefaultItemizedOverlay poiOverlay = new DefaultItemizedOverlay(icon);
OverlayItem loc1 = new OverlayItem(new GeoPoint (1.308763,103.777321), "area","area");
poiOverlay.addItem(loc1);
OverlayItem loc2 = new OverlayItem(new GeoPoint (1.309498,103.777101), "area","area");
poiOverlay.addItem(loc2);
OverlayItem loc3 = new OverlayItem(new GeoPoint (1.311531,103.778318), "area","area");
poiOverlay.addItem(loc3);
OverlayItem loc4 = new OverlayItem(new GeoPoint (1.308071,103.77841), "area","area");
poiOverlay.addItem(loc4);
poiOverlay.setOnFocusChangeListener(new ItemizedOverlay.OnFocusChangeListener() {
#Override
public void onFocusChanged(ItemizedOverlay Overlay, OverlayItem newFocus) {
// when focused item changes, recenter map and show info
map.getController().animateTo(newFocus.getPoint());
Toast.makeText(map.getContext().getApplicationContext(), newFocus.getTitle() + ": " +
newFocus.getSnippet(), Toast.LENGTH_SHORT).show();
int lastTouchedIndex = poiOverlay.getLastFocusedIndex();
if(lastTouchedIndex>-1){
OverlayItem tapped = poiOverlay.getItem(lastTouchedIndex);
annot.showAnnotationView(tapped);
}
}
});
map.getOverlays().add(poiOverlay);
map.invalidate();
}
}
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.
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.