In my application I have a map-activity which uses OSMDroid. I've managed to manually add markers with Overlay to display different locations but I want to implement a function where a marker will be created when you either double-tap or longpress.
I've scavenged the web for some kind of good explanation since I've troubles understanding the documentation of OSMDroid and their bonuspack.
Here is my relevant code:
public class Map extends Activity{
MapView mapView;
MapController mapController;
ArrayList<OverlayItem> overlayItemArray;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
mapView = (MapView) this.findViewById(R.id.mapView);
mapView.setBuiltInZoomControls(true);
mapView.setMultiTouchControls(true);
mapController = (MapController) mapView.getController();
mapController.setZoom(14);
GeoPoint startPoint = new GeoPoint(58.4109, 15.6216);
mapController.setCenter(startPoint);
// Create an ArrayList with overlays to display objects on map
overlayItemArray = new ArrayList<OverlayItem>();
// Create som init objects
OverlayItem linkopingItem = new OverlayItem("Linkoping", "Sweden",
new GeoPoint(58.4109, 15.6216));
OverlayItem stockholmItem = new OverlayItem("Stockholm", "Sweden",
new GeoPoint(59.3073348, 18.0747967));
// Add the init objects to the ArrayList overlayItemArray
overlayItemArray.add(linkopingItem);
overlayItemArray.add(stockholmItem);
// Add the Array to the IconOverlay
ItemizedIconOverlay<OverlayItem> itemizedIconOverlay = new ItemizedIconOverlay<OverlayItem>(this, overlayItemArray, null);
// Add the overlay to the MapView
mapView.getOverlays().add(itemizedIconOverlay);
}
Related
I am using mylocationoverlay to give my current location, however, its not exactly accurate. Sometimes it is up to 20 meters wrong. I want to be able to move the location marker to my exact position. When using the following code, when I press on the marker, the map moves and the marker stays in the same place. How do I keep the map static and allow a user to move the marker?
public class selectmap extends MapActivity {
private MapView mapView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.selectmap);
mapView = (MapView) findViewById(R.id.map_view);
mapView.setBuiltInZoomControls(true);
final MyLocationOverlay myLocationOverlay = new MyLocationOverlay(this, mapView);
myLocationOverlay.enableMyLocation();
mapView.getController().setZoom(17);
mapView.getOverlays().add(myLocationOverlay);
mapView.postInvalidate();
}
}
You have to write your own implementation to handle this scenario. Here is an example application where you can move the markers
https://github.com/commonsguy/cw-advandroid/tree/master/Maps/NooYawkTouch/
I have try to create application of open street map . but map is not display only grid is display. i have use a these code in my application.
public class MainActivity extends Activity{
private MapView mapView;
private MapController mapController;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mapView = (MapView) this.findViewById(R.id.mapview);
mapView.setTileSource(TileSourceFactory.MAPNIK);
mapView.setBuiltInZoomControls(true);
mapView.setMultiTouchControls(true);
mapController = this.mapView.getController();
mapController.setZoom(2);
GeoPoint gPt = new GeoPoint(51500000, -150000);
//Centre map near to Hyde Park Corner, London
mapController.setCenter(gPt);
}
}
thanx in advance.
please.. any body tell me what is wrong in my code..
Do you have your osmdroid library into "libs" folder?
Mine is osmdroid-android-3.0.8.jar
I don't see anything wrong to just show the map... :S
I've a mapView on which I managed to get an overlay onLongPress with the help of GestureListener. What I actually want is, I want to add markers (with same icon) one by one. Like I want to mark different positions on map (not all at once). Any help with this would be great as am newbie with MapView and Overlays.
Use ItemizedOverlay class to add drawable on specific longitudes and latitudes.
In your MapActivity write
GeoPoint your_point = (Provide geopoint information here);
Drawable drawable =
this.getResources().getDrawable(R.drawable.your_point_image);
HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(
drawable, MapsActivity.this, mapView);
GeoPoint point = new GeoPoint((int) (your_point.getLat() * 1E6),
(int) (your_point.getLon() * 1E6));
OverlayItem overlayitem1 = new OverlayItem(your_point);
itemizedoverlay.addOverlay(overlayitem1);
mapView.getOverlays().add(itemizedoverlay);
Where
HelloItemizedOverlay extends ItemizedOverlay{
public HelloItemizedOverlay(Drawable defaultMarker, Activity context,
MapView mapView) {
super(boundCenterBottom(defaultMarker));
mContext = context;
this.mapView = mapView;
}
}
I did it this way and it works.
i am trying to make the application display my location on google maps with a geapoint, but it always opens the same place when opening the application on my phone, which is the value i added in the code as an initial value for the testing on the compiler.
how can i make it display my location(make it change the Latitude and longitude according to my place)?
here is the code:
public class AndroidGoogleMapsActivity extends MapActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Displaying Zooming controls
MapView mapView = (MapView) findViewById(R.id.mapView);
mapView.setBuiltInZoomControls(true);
/**
* Changing Map Type
* */
mapView.setSatellite(true); // Satellite View
// mapView.setStreetView(true); // Street View
mapView.setTraffic(true); // Traffic view
/**
* showing location by Latitude and Longitude
* */
MapController mc = mapView.getController();
double lat = Double.parseDouble("31.894178");
double lon = Double.parseDouble("35.872694");
GeoPoint geoPoint = new GeoPoint((int)(lat * 1E6), (int)(lon * 1E6));
mc.animateTo(geoPoint);
mc.setZoom(15);
mapView.invalidate();
/**
* Placing Marker
* */
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.mark_red);
AddItemizedOverlay itemizedOverlay =
new AddItemizedOverlay(drawable, this);
OverlayItem overlayitem = new OverlayItem(geoPoint, "Hello", "Sample Overlay item");
itemizedOverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedOverlay);
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
}
You're never calling the actual GPS which is why it's reverting to the ones you programmed in. This is pretty well documented in other places, but here's a good place to start: http://about-android.blogspot.com/2010/04/find-current-location-in-android-gps.html
See Location detection ; that's the easy way to do it afaik, and it worked for me. Cheers!
i have a mapview and overlay items which when tap on shows popuo with text. what I want is when user taps on map outside of these overlay items or popup hide the popup if its currently visible.
The only way to do it (as I know), is adding new overlay to your map, witch serves as the popup. When you tap outside of the overlay, just remove it from overlay list.
This means you have to draw and handle events for your popup by yourself.
Ive been trying to acheive the same, this is exactly what i want :-
http://proxy.latest.xuemath.appspot.com/img?s=aR.1f.1be6ifej.2ec/_Fbk8IlxNQXM/S-ruq97dRWI/AAAAAAAAELY/s-o3onSReiU/s400/kml_google_maps_v3.bmp
i have implemented googleMapView with overlays, i have one issue i want to show a popup when clicked on each overlay, and when i click on another overlay the previous popus should disappear and new one should appear at the clicked location(ie projection points). And popup shouldnt appear when i click anywhere else on screen.Im using onTap event to record onclick. (map_overlay) is the layout that i want to show when someone click a projection point on the map. The code is below and map_overlay.xml could be any file.
Class: ItemizedOverlay:-
public boolean onTap(GeoPoint p, MapView mapView) {
LayoutInflater inflater = (LayoutInflater)cContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LayoutParams lp = new MapView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT, p, LayoutParams.WRAP_CONTENT);
LinearLayout view = (LinearLayout)inflater.inflate(R.layout.map_overlay, null);
mapView.removeView(view);
mapView.invalidate();
mapView.addView(view,lp);
mapView.invalidate();
return true;
}
Below is the Main class in which im displaying projection points which is working fine and im calling I have implemented the onTap event in another class as shown above:
public class MapView extends MapActivity{
private ArrayList overlayItem ;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.large_mapview);
mapView = (MapView) findViewById(R.id.mapview);
// mapView.setBuiltInZoomControls(true);
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.mappointer2);
ItemizedOverlay itemizedoverlay = new ItemizedOverlay(drawable,this);
OverlayItem overlayitem;
GeoPoint point;
double lat;
double lng;
for (int i = 0; i < overlayItem.size(); i++) {
lat = Double.parseDouble(overlayItem.get(i).getLatitude());
lng = Double.parseDouble(overlayItem.get(i).getLongitude());
point = new GeoPoint((int) (lat * 1E6),(int) (lng * 1E6));
overlayitem = new OverlayItem(point, i+"".toString(), overlayItem.get(i).getDetails().toString());
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
}
mapView.invalidate();
}
}