How to set MapView Zoom Level in onCreate() ? - android

I'm developing a OneMap Android application and the code is as follows:
private MapView map;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
map = (MapView) findViewById(R.id.map);
map.addLayer(new ArcGISTiledMapServiceLayer(Practice_3.this,
"http://www.onemap.sg/ArcGIS/rest/services/BASEMAP/MapServer"));}
I'm lost at figuring out how to set the zoom level when the map is first loaded on the emulator as on load, the map is too small.
Could anyone help me with this?
Any help would be greatly appreciated.

public class MyMap_MapControllerActivity extends MapActivity {
private MapView mapView;
//private MapController mapController;
MapView.LayoutParams lp;
int y = 10;
int x = 10;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView)findViewById(R.id.map_view);
mapView.setSatellite(true);
mapView.setStreetView(true);
mapView.setTraffic(true);
GeoPoint center = mapView.getMapCenter();
int latSpan = mapView.getLatitudeSpan();
int longSpan = mapView.getLongitudeSpan();
lp = new MapView.LayoutParams(MapView.LayoutParams.WRAP_CONTENT,
MapView.LayoutParams.WRAP_CONTENT,
x, y,
MapView.LayoutParams.TOP_LEFT);
View zoomControls = mapView.getZoomControls();
mapView.addView(zoomControls, lp);
mapView.displayZoomControls(true);
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}

Firstly you need to know your current location, now you can use new location api, here is the guide.
then you can add below command under Onconnect()
LatLng latlng = new LatLng(location.getLatitude(),location.getLongitude());
map.moveCamera(CameraUpdateFactory.newLatLng(latlng));
map.animateCamera(CameraUpdateFactory.zoomTo(2.0f));
but remember .connect() LocationClient under onCreate().
here is the error message you will get if you not follow
Android: at com.google.android.gms.internal.u.y(Unknown Source)

It looks like you`re using ArcGIS maps.
Please use the below sample code to set zoom level on the launch of the maps.
<com.esri.android.map.MapView
android:id="#+id/map"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
mapoptions.MapType="Topo"
mapoptions.ZoomLevel="13"
mapoptions.center="33.666354, -117.903557" />
you can also set in activity file,
MapOptions options = new MapOptions(MapType.TOPO, 23, -110, 9);
Let me know if it works for you.

Related

Create marker (overlay) on tap/click in OSMDroid

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);
}

How do I change the result of mylocationoverlay?

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/

Open street Map is Not Display in OSM map view

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

How to create a static Mapview that recognize an onTap method

I'm trying to develop a layout that shows a Google Map with a route drawn on a Overlay layer associated with this map. I want this mapView to be a small static map that shows, as a thumbnail, the main route the user followed, and then if the user clicks on it, an intent takes you to a different activity displaying the map in full screen with the route and all the zoom functionalities.
The thing is that although I override the onTap method of the Overlay setting there the intent to the new activity, it only works if I set the MapView as setEnabled(true), but if I do so, then the thumbnail map can be dragged and moved by the user.
I'm sorry if it is not clear enough, but I don't know how to explain it better.
Thanks in advance
This is my customized class which extends Overlay and overrides the onTap method:
class MapOverlay extends Overlay {
#Override
public boolean onTap(GeoPoint p, MapView mapView) {
Intent i = new Intent(getApplicationContext(),
RouteMapActivity.class);
startActivity(i);
return false;
};
And this is my onCreate method:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detailrun_activity);
map = (MapView) findViewById(R.id.mvMain);
map.setEnabled(true);
map.setClickable(true);
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = map.getOverlays();
projection = map.getProjection();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
double lat = 28.063119 * 1E6, longi = -82.41128 * 1E6;
GeoPoint point = new GeoPoint((int) lat, (int) longi);
MapController myMapController = map.getController();
myMapController.setCenter(point);
}
For now I'm only drawing a straight line between two GeoPoints, and I don't get any logcat errors.
The map with the route is indeed drawn, and the overrided onTap method works, but it seems it only recognizes the tap if my mapView is defined as enabled and clickable, but if I do so, then the user can also move and drag the map as long as he holds pressing the screen.
you can get static google map image as per your location by this link. that you can display in imageview and click on that you can target new activity which have MapView.. i think it may be a good option for you.

how can i implement my own double-tap in mapview?

I have been working on an offline map using OSMdroid
I have map tiles of two zoom levels namely 12 and 15
Now the usual double tap zooms the mapview by 1 level
What i m trying to do is to setZoomLevel() as 15 after the user double taps on the map,
Is it possible?
I tried using onGestureListener , but somehow it is not working
Any hint or clues in that direction or sample codes would be a big help , Thanks
I had a similar requirement with the Osmdroid MapView, in that I didn't want it to do the 'centre on the double tapped location and zoom in' default functionality. I wanted it to pop up a Toast. In my case I had an overlay on top of the MapView, so I just had the overlay consume the double tap in its onDoubleTap method. For your purposes you could just add an overlay which draws nothing but has its own double tap functionality.
So at the end of your onCreate, you could add the overlay. This little app seems to demonstrate what you want - (you'll need to add conditional code for checking zoom level and other tinkering):
public class OsmdroidDemoMap extends Activity {
private MapView mMapView;
private MapController mMapController;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.osm_main);
mMapView = (MapView) findViewById(R.id.mapview);
mMapView.setTileSource(TileSourceFactory.MAPNIK);
mMapView.setBuiltInZoomControls(true);
mMapController = mMapView.getController();
mMapController.setZoom(13);
GeoPoint gPt = new GeoPoint(51500000, -150000);
mMapController.setCenter(gPt);
DummyOverlay dumOverlay = new DummyOverlay(this);
List<Overlay> listOfOverlays = mMapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(dumOverlay);
}
public class DummyOverlay extends org.osmdroid.views.overlay.Overlay {
public DummyOverlay(Context ctx) {
super(ctx); // TODO Auto-generated constructor stub
}
#Override
protected void draw(Canvas c, MapView osmv, boolean shadow) {}
#Override
public boolean onDoubleTap(MotionEvent e, MapView mapView) {
// This stops the 'jump to, and zoom in' of the default behaviour
int zoomLevel = mMapView.getZoomLevel();
mMapController.setZoom(zoomLevel + 3);
return true;// This stops the double tap being passed on to the mapview
}
}
If you need to manage the double-tap in an MapView, i suggest you to check this thread:
android maps: How to Long Click a Map?
You can see my answer and how i adapted mapview-overlay-manager.
you can override the zoom control with its event, for that you need to define into the xml layout or you get the zoom control from the mapview
ZoomControls zoomControls = (ZoomControls)findViewById(R.id.zoomControl);
View zoomOut = zoomControls.getChildAt(0);
zoomOut.setBackgroundDrawable(getResources().getDrawable(R.drawable.zoom_out_icon));
zoomOut.setPadding(5, 5, 5, 5);
View zoomIn = zoomControls.getChildAt(1);
zoomIn.setBackgroundDrawable(getResources().getDrawable(R.drawable.zoom_in_icon));
zoomIn.setPadding(5, 5, 5, 5);
zoomControls.setOnZoomInClickListener(new View.OnClickListener() {
public void onClick(View v) {
mc.zoomIn();
mapView.invalidate();
}
});
zoomControls.setOnZoomOutClickListener(new View.OnClickListener() {
public void onClick(View v) {
mc.zoomOut();
mapView.invalidate();
}
});
now you can implement your own zoom control
In the latest OSMdroid library 4.0 has inbuilt automatic double tap zoom in. But nothing is better than having a zoom control.
you can use just add below line
mMapView.setMultiTouchControls(true);

Categories

Resources