I'm trying to add an overlay for myLocation in Android. The map displays, but the overlay does not. I did get the overlay to appear using a separate class that extends ItemizedOverlay. I'm wondering of there is a way to display this individual point without creating a separate class?
Attached is the source code for the activity class.
public class WalkAbout extends MapActivity {
//for Hello_mapview
List<Overlay> mapOverlays;
Drawable drawable;
private MapView m_vwMap;
private MapController m_mapController;
private PathOverlay m_pathOverlay;
private MyLocationOverlay m_locationOverlay;
private ArrayList<GeoPoint> m_arrPathPoints;
private ArrayList<OverlayItem> m_arrPicturePoints;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initLocationData();
initLayout();
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
private void initLocationData() {
// TODO
}
private void initLayout() {
//instantiate XML File into corresponding view objects.
//Then inflate new view from XML resource.
setContentView(R.layout.map_layout);
MapView m_vwMap = (MapView)findViewById(R.id.mapview);
m_vwMap.setBuiltInZoomControls(true);
m_vwMap.setSatellite(true);
//retrieve list of overlay objects
mapOverlays=m_vwMap.getOverlays();
//set market for overlays
drawable=this.getResources().getDrawable(R.drawable.item);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight());
//create OverlayItem with my location
m_locationOverlay= new MyLocationOverlay(this, m_vwMap);
//enable market to set location and direction
m_locationOverlay.enableCompass();
m_locationOverlay.enableMyLocation();
mapOverlays.add(m_locationOverlay);
}
}
You need to call invalidate() on your m_vwMap so it will redraw itself.
Related
I'm using google map in my app , the code runs on device perfectly but the map does not load ! I just see a white screen ! what's the problem ?
here is the code , please help !
public class MyMap extends MapActivity {
private MapView map;
private MapController controller;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initMapView();
initMyLocation();
}
/** Find and initialize the map view. */
private void initMapView() {
map = (MapView) findViewById(R.id.map);
controller = map.getController();
map.setSatellite(true);
map.setBuiltInZoomControls(true);
}
/** Start tracking the position on the map. */
private void initMyLocation() {
final MyLocationOverlay overlay = new MyLocationOverlay(this, map);
overlay.enableMyLocation();
//overlay.enableCompass(); // does not work in emulator
overlay.runOnFirstFix(new Runnable() {
public void run() {
// Zoom in to current location
controller.setZoom(8);
controller.animateTo(overlay.getMyLocation());
}
});
map.getOverlays().add(overlay);
}
#Override
protected boolean isRouteDisplayed() {
// Required by MapActivity
return false;
}
}
put your map api key in xml here is tutorial to get map api key
Create your google map api key and pest in maplayout.xml
android:apiKey=" your api key"
which is freely avaliable
I've read a lot of articles but none of them could fix my problem of not calling the onCreate-method in the class XMLParsingExample.
The log-statement in the onCreate didn't show output and tracing shows that the class is exited after boolean finished=false and thus not running the onCreate.
Here the codes:
public class MyMap extends MapActivity {
private MapView mapView;
private MapController mc;
private OverlayItem overlayItem;
private List<Overlay> mapOverlays;
private Drawable drawable;
private Drawable drawable2;
private MyItemizedOverlay itemizedOverlayMyLoc;
private MyItemizedOverlay itemizedOverlayRust;
private LocationManager locMgr;
private MyLocationListener locLstnr;XMLParsingExample mXMLParsingExample;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mc = mapView.getController();
mapView.setBuiltInZoomControls(true);
locMgr = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
locLstnr = new MyLocationListener();
locMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locLstnr);
mapOverlays = mapView.getOverlays();
// first overlay
drawable = getResources().getDrawable(R.drawable.marker2);
itemizedOverlayMyLoc = new MyItemizedOverlay(drawable, mapView);
// LAT LONG
GeoPoint uwLoc = new GeoPoint((int)(52.22778*1E6),(int)(6.10428*1E6));
overlayItem = new OverlayItem(uwLoc, "Uw locatie", "http://www.nu.nl");
itemizedOverlayMyLoc.addOverlay(overlayItem);
mapOverlays.add(itemizedOverlayMyLoc);
// Rustpunten overlay
drawable2 = getResources().getDrawable(R.drawable.rmarker3);
itemizedOverlayRust = new MyItemizedOverlay(drawable2, mapView);
mXMLParsingExample = new XMLParsingExample();
and here the class which where the oncreate isn't called:
public class XMLParsingExample extends Activity {
/** Create Object For SiteList Class */
public SitesList sitesList = null;
public ProgressDialog progressDialog;
boolean finished=false;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.i("onCreate", "onCreate started");
}
Starting a new Activity is not done by instantiating it (new XMLParsingExample();), but with an intent, for example:
Intent intent = new Intent(this, XMLParsingExample.class);
startActivity(intent);
Take a look at the here.
Binyamin Sharet is correct.
I think you are confusing a creator method, which does get called when you allocate an object, and onCreate(), which is an Android lifecycle callback function that gets called automatically by the framework at the appropriate time.
A creator function doesn't usually have 'create' in its name; it shares the name of the class whose object you are instantiating. In your case, the creator would be called XMLParsingExample().
For more information about Android lifecycle callbacks, see http://developer.android.com/guide/topics/fundamentals/activities.html.
In order to detect map movements and gestures and use the lazy loading support for my map items I'm trying to work with this library: http://code.google.com/p/mapview-overlay-manager/.
I've setup a map attached the overlayManager and the events are coming through just fine. I can throw a toast from the listener just fine. When I get Application context it is not null.
I'm stuck trying to launch an intent from the ManagedOverlay class. Specifically in the onDoubleTap method below I'm trying to launch an intent and I get this error message:
Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
I think I generally understand that I need to call back to the MapActivity subclass and have it launch the intent or I need to do something with the context differently. I'm having trouble ironing out the specifics however. Any assistance appreciated.
public class SiteMapRev2 extends MapActivity {
private MapView mapView;
private OverlayManager overlayManager;
private MapController mapController;
private MyLocationOverlay userLocationOverlay;
private ArrayList<SiteSummary> sitesRoster = null;
private Drawable siteIcon;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
overlayManager = new OverlayManager(this, mapView);
sitesRoster = new ArrayList<SiteSummary>();
userLocationOverlay = new MyLocationOverlay(this, mapView);
mapView.getOverlays().add(userLocationOverlay);
mapController = mapView.getController();
mapController.setZoom(14);
siteIcon = this.getResources().getDrawable(R.drawable.marker2);
}
#Override
public void onStart() {
super.onStart();
Drawable defaultmarker = getResources().getDrawable(R.drawable.marker2);
ManagedOverlay managedOverlay = overlayManager.createOverlay("sites", defaultmarker);
managedOverlay.setOnOverlayGestureListener(new ManagedOverlayGestureDetector.OnOverlayGestureListener(){
public boolean onDoubleTap(MotionEvent arg0, ManagedOverlay arg1,
GeoPoint arg2, ManagedOverlayItem arg3) {
if (arg3 == null) {
return false;
}
else {
**SiteOverlayItem thisItem = (SiteOverlayItem) arg3;
String siteIDAsString = Integer.toString(thisItem.getSiteID());
Context c = getApplicationContext();
Intent showSiteDetails = new Intent(c,SiteDetailActivity.class);
Log.d(toString(), "intent = " + showSiteDetails.toString());
showSiteDetails.setData(Uri.parse(siteIDAsString));
c.startActivity(showSiteDetails);
return true;**
}
}
Instead of getting the application context, I would do this:
SiteMapRev2.this.startActivity(showSiteDetails);
which starts the activity from your map activity as you normally would.
Set this Flag to your intent, Logcat is too smart, try to understand what is says to you ;)
showSiteDetails.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
I have a map activity that has many pins of map, and when I click a pin, a custom balloon opens, showing some information about that pin. Also, I have a search bar, where if you type the name of a knob, the info appears there, but I want it to go to that searched pin.
Example: on the map you have different vegetables pins, and when you search carrot, the search list will show the element carrot, and when you click on it, the balloon for the carrot pin will inflate. So, my question is : is there some sort of OnTap() void method ? I know, that OnTap(int index) returns a boolean.
create your own itemized overlay, and override the onTap method, and in your main class, make an instance of the itemized overlay, and call overlay.onTap(point)
Sample code:
public class MyItemizedOverlay<Item> extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> m_overlays;
private MapView mapView;
final MapController mc;
public MyItemizedOverlay(Drawable defaultMarker, MapView mapView) {
super(boundCenterBottom(defaultMarker), mapView);
m_overlays = new ArrayList<OverlayItem>();
mc = mapView.getController();
populate();
}
public void addOverlay(OverlayItem overlay) {
m_overlays.add(overlay);
setLastFocusedIndex(-1);
populate();
}
public ArrayList<OverlayItem> getOverlays() {
return m_overlays;
}
public final boolean onTap(int index) {
GeoPoint point;
point = createItem(index).getPoint();
mc.animateTo(point);
return true;
}
...
}
In the main class
public class Main extends MapActivity {
private MapView mapView;
private List<Overlay> mapOverlays;
private MyItemizedOverlay overlay;
private Drawable pin;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
doAction();
}
private void doAction() {
mapView = (MapView)findViewById(R.id.map_view);
pin = res.getDrawable(R.drawable.pin);
overlay = new MyItemizedOverlay(pin, mapView);
GeoPoint point = new GeoPoint((int)(7*1E6),(int)(42*1E6));
overlayItem = new OverlayItem(point, "title", "text");
overlay.addOverlay(overlayItem);
mapOverlays = mapView.getOverlays();
mapOverlays.add(overlay);
//we tap the point here
overlay.onTap(0);
}
}
I followed the instructions from the google hellomapview tutorial. I get a working mapview etc. But the two items that are added to the map are not shown. It seems they are there somewhere because tapping at the specified location shows the message that was added to the items.
Edit
Here is my source code. It should be very close to the google tutorial source code.
public class MapOverlay extends ItemizedOverlay<OverlayItem> {
private List<OverlayItem> overlays = new ArrayList<OverlayItem>();
private Context context;
public MapOverlay(Drawable defaultMarker, Context context) {
super(defaultMarker);
overlays = new ArrayList<OverlayItem>();
this.context = context;
}
#Override
protected OverlayItem createItem(int i) {
return overlays.get(i);
}
#Override
public int size() {
return overlays.size();
}
public void addOverlay(OverlayItem overlay) {
overlays.add(overlay);
this.populate();
}
#Override
protected boolean onTap(int index) {
OverlayItem item = overlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(this.context);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
}
public class MapsActivity extends MapActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
MapOverlay overlay = new MapOverlay(this.getResources().getDrawable(
R.drawable.androidmarker), this);
overlay.addOverlay(new OverlayItem(new GeoPoint(19240000,-99120000), "Blubb", "See?"));
mapView.getOverlays().add(overlay);
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
}
Is the source code from the google tutorial available somewhere?
The problem is that I forgot to set the bounds of the drawable. It seems that if the mapview doesn't know how to align the image it won't show it at all.
I changed the first line in my constructor from:
super(defaultMarker);
to
super(boundCenterBottom(defaultMarker));
and know its working perfect.
At the same time, I have no idea how to help you directly.
Here are links to various editions of a project that definitely work with overlays, perhaps they will help.