Purpose of the `aBaseUrl` parameter in the class OnlineTileSourceBase? - android

I don't understand the purpose of the aBaseUrl parameter in the class OnlineTileSourceBase. My reason for inquiring is that I am trying to get offline tiles to display and thus far can't get it to work. I see the overlay I created, but no map data (just that grey grid) and I wonder if I need to set aBaseUrl to something appropriate.
The data is on the device is in sdcard/osmdroid/tiles/Mapnik/.
Mapnik contains folders 0, 1, ... 14, which themselves contains folders which contain .jpg files.
Online, this code works (removing the call setUseDataConnection(false) and setting tile source to MAPNIK). Based on code by #nightfixed here.
public class MapActivity extends AppCompatActivity {
final private int MIN_ZOOM_LEVEL = 0;
final private int MAX_ZOOM_LEVEL = 14;
final private int TILE_SIZE = 256;
final private String IMAGE_EXTENSION = ".jpg";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
CustomTileSource tileSource = new CustomTileSource ("Default",
MIN_ZOOM_LEVEL,
MAX_ZOOM_LEVEL,
TILE_SIZE,
IMAGE_EXTENSION,
CustomTileSource.TILE_URL);
final MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setTileSource(tileSource);
// mapView.setTileSource(TileSourceFactory.MAPNIK);
mapView.setUseDataConnection(false); // keeps the mapView from loading online tiles using network connection.
}
}
public class CustomTileSource extends OnlineTileSourceBase {
public static String[] TILE_URL = {"my_url"};
public CustomTileSource (String aName,
int aZoomMinLevel,
int aZoomMaxLevel,
int aTileSizePixels,
String aImageFilenameEnding,
String[] urlArray) {
super(
aName,
aZoomMinLevel,
aZoomMaxLevel,
aTileSizePixels,
aImageFilenameEnding,
urlArray);
}
// returns the url for each tile, depending on zoom level
// this is where I changed the return statement to take the first url from the string array of urls
#Override
public String getTileURLString(MapTile aTile) {
return TILE_URL[0] + aTile.getX() + "+" + aTile.getY() + "+" + aTile.getZoomLevel();
}
}

I suggest you to follow closely this post: Download maps for osmdroid
No need for CustomTileSource, just use mapView.setTileSource(TileSourceFactory.MAPNIK);
If your tiles are in "Mapnik" dir (sdcard/osmdroid/tiles/Mapnik) then TileSource aName should be set to "Mapnik", not to "Default".
When offline, aBaseUrl doesn't matter.

aBaseUrl is basically the primary url to the online map server. For example
http://tiles.mymapserver.com/mapdata (note, complete fictitious)
After the aBaseUrl, osmdroid calculates with tiles to load via various algorithms and then appends something like /Z/X/Y.jpg to the end of the aBaseUrl string when downloading.

Related

Calculator based on one field

I'm working on Android Studio, to make one field calculator
It will be just one Text field, button, and another text view.
If I put "2+5*6" for example it must understand the operation.
Can anyone help me?
Check out my code please
public class MainActivity extends AppCompatActivity {
public static final String equation = "[0-9]";
String opr = " ";
int[] result;
int castedInt;
String temp;
String[] separated;
EditText txte;
TextView txtv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void buResult(View view) {
txte = (EditText)findViewById(R.id.editText);
for (int i = 0; i < txte.length(); i++) {
if (Pattern.matches(txte.toString(), equation)) {
separated[i] = txte.toString();
temp = separated[i];
castedInt = Integer.parseInt(temp.toString());
result[i] = castedInt;
}
else {
opr = txte.toString();
}
txtv.setText(result[i] + opr + result[i]);
}
}
}
You should implement reverse polish notation for string.
Example code could be find here https://codereview.stackexchange.com/questions/120451/reverse-polish-notation-calculator-in-java
As defined in the documentation regex must be a regular expression. You need to transform the user input into a regular expression.
Pattern.matches(String regex, CharSequence input)
Also you are always using the whole user input:
separated[i] = txte.toString();
All "separated" indexes will contain the same.
If it's Ok to use language support (instead of creating algorithm from scratch) you can use like this:
import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;
...
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");
txtv.setText(engine.eval(txte.toString()));
*credits : stackoverflow.com/a/2605051/936786

Is it a good thing to write logic based on the hex value of id generated in R.java android

This what I have seen in an android application. They have a number of image buttons with ids
R.java :
public static final int img1=0x7f090080;
public static final int img2=0x7f090081;
public static final int img3=0x7f090082;
public static final int img4=0x7f090083;
public static final int img5=0x7f090084;
public static final int img6=0x7f090085;
public static final int img7=0x7f090086;
public static final int img8=0x7f090087;
In one of the activity they are traversing a for loop like below:
for (int i = 0; i < NoOfButtons; i++) {
if (i == pos) {
((ImageView) vi.findViewById(R.id.img1 + i)).setImageResource(R.drawable.circular_pagination_red);
} else {
((ImageView) vi.findViewById(R.id.img1 + i)).setImageResource(R.drawable.circular_pagination_brown);
}
I want to know how much safe and advisable it is.
One thing this is working absolutely fine. I been a part of this from months and never seen a problem in this logic. But still it irks me a bit.
Note : I am not getting any error and I know the alternate solution also. My only concern is if it is not advisable/safe I want to know why? Another is scenarios where it can create havoc for me. I have a good understanding about R.java.
Though this might work OK most of the times, this is definitely not advisable. The R class is generated automatically thus you have no control over it and it could change. There is a solution to this problem using a typed array in resources. Check for example this answer.
You might want to use reflection.
Add this method to your code:
protected final static int getResourceID
(final String resName, final String resType, final Context ctx)
{
final int ResourceID =
ctx.getResources().getIdentifier(resName, resType,
ctx.getApplicationInfo().packageName);
if (ResourceID == 0)
{
throw new IllegalArgumentException
(
"No resource string found with name " + resName
);
}
else
{
return ResourceID;
}
}
And use it like this:
int myID =
getResourceID("your_resource_name", "drawable", getApplicationContext());
Note: no path (and no extension, in case of images).

How to create osmdroid XYTileSource for offline tiles in version 4.1?

I have offline osmdroid maps working using version 4.0. Upgrading to 4.1, they no longer work. I have narrowed the problem down to the XYTileSource, in which aBaseUrl changed from being a string in 4.0 to and array in 4.1. How do I get offline tiles to work in 4.1?
Old 4.0 code that worked. The tiles are in /sdcard/osmdroid/tiles.zip
XYTileSource ts = new XYTileSource ( "tiles",
ResourceProxy.string.offline_mode,
13,
17,
256,
".png",
"http://127.0.0.1");
mapView = (MapView) findViewById(R.id.mapview);
mapView.setTileSource(ts);
mapView.setMultiTouchControls(true);
mapView.setBuiltInZoomControls(false);
mapView.setUseDataConnection(false);
mapView.getController().setZoom(15);
GeoPoint point = new GeoPoint(40.715,-73.945);
mapView.getController().setCenter(point);
I tried changing it to this, but it doesn't work.
String[] urls = {"http://127.0.0.1"};
XYTileSource ts = new XYTileSource ( "tiles",
ResourceProxy.string.offline_mode,
13,
17,
256,
".png",
urls);
I tried to provide a full answer here:
Download maps for osmdroid
If you have an "old" tiles.zip, open it, and check:
the root directory name => put it as the "aName" of XYTileSource constructor (is it really "tiles"?)
the tiles images extension => put it as the aImageFileNameEnding (is it really ".png"?)
The aResourceId and aBaseUrl params are not used for zip files.
I see that you are using XYTileSource, which by default extends OnlineTileSourceBase.
I have found a workaround for the Url issue, by creating a CustomTileSource class. Something like below:
public class CustomTileSource extends OnlineTileSourceBase {
public static String[] TILE_URL = {"my_url"};
//constructor is default - I changed nothing here
public CustomTileSource (String aName, string aResourceId, int aZoomMinLevel, int aZoomMaxLevel,
int aTileSizePixels, String aImageFilenameEnding, String[] url) {
super(
aName,
aResourceId,
aZoomMinLevel,
aZoomMaxLevel,
aTileSizePixels,
aImageFilenameEnding,
url);
// TODO Auto-generated constructor stub
}
/**
* returns the url for each tile, depending on zoom level
*/
//this is where I changed the return statement to take the first url from the string array of urls
#Override
public String getTileURLString(MapTile aTile) {
return TILE_URL[0] + aTile.getX() + "+" + aTile.getY() + "+" + aTile.getZoomLevel();
}
}
In my code, where I need to instantiate the tilesource, I use:
CustomTileSource tileSource = new CustomTileSource ("Default", ResourceProxy.string.offline_mode, MIN_ZOOM_LVL, MAX_ZOOM_LVL, DEFAULT_TILE_SIZE, TILE_FORMAT, CustomTileSource.TILE_URL);
//MIN_ZOOM_LVL, MAX_ZOOM_LVL, DEFAULT_TILE_SIZE, TILE_FORMAT are constants that I defined elsewhere
Hope it helps.

Show source code in TextView correctly indented and parsed

Is it possible to parse HTML code in a verbatim mode or something similar so that the source code fragments that eventually may appear (enclosed between pre and code HTML tags) can be displayed properly?
What I want to do is show source code in a user-friendly mode (easy to distinguish from the rest of the text, keep indentation, etc.), as Stack Overflow does :)
It seems that Html.fromHtml() supports only a reduced subset of HTML tags.
TextView will never succeed supporting all the html formating and styling you would want it to. Use WebView instead.
TextView is native and more lightweight, but exactly because of its lightweightedness it will not understand some of the directives you describe.
Finally I preparsed by myself the HTML code received, since Html.fromHtml does not support the pre and code tags, y replaced them with my custom format and pre-parsed the code inside those tags replacing "\n" with <br/> and " " with .
Then I send the results to Html.fromHtml, and the result is just fine:
public class HtmlParser {
public static Spanned parse(String text) {
if (text == null) return null;
text = parseSourceCode(text);
Spanned textSpanned = Html.fromHtml(text);
return textSpanned;
}
private static String parseSourceCode(String text) {
if (text.indexOf(ORIGINAL_PATTERN_BEGIN) < 0) return text;
StringBuilder result = new StringBuilder();
int begin;
int end;
int beginIndexToProcess = 0;
while (text.indexOf(ORIGINAL_PATTERN_BEGIN) >= 0) {
begin = text.indexOf(ORIGINAL_PATTERN_BEGIN);
end = text.indexOf(ORIGINAL_PATTERN_END);
String code = parseCodeSegment(text, begin, end);
result.append(text.substring(beginIndexToProcess, begin));
result.append(PARSED_PATTERN_BEGIN);
result.append(code);
result.append(PARSED_PATTERN_END);
//replace in the original text to find the next appearance
text = text.replaceFirst(ORIGINAL_PATTERN_BEGIN, PARSED_PATTERN_BEGIN);
text = text.replaceFirst(ORIGINAL_PATTERN_END, PARSED_PATTERN_END);
//update the string index to process
beginIndexToProcess = text.lastIndexOf(PARSED_PATTERN_END) + PARSED_PATTERN_END.length();
}
//add the rest of the string
result.append(text.substring(beginIndexToProcess, text.length()));
return result.toString();
}
private static String parseCodeSegment(String text, int begin, int end) {
String code = text.substring(begin + ORIGINAL_PATTERN_BEGIN.length(), end);
code = code.replace(" ", " ");
code = code.replace("\n","<br/>");
return code;
}
private static final String ORIGINAL_PATTERN_BEGIN = "<pre><code>";
private static final String ORIGINAL_PATTERN_END = "</code></pre>";
private static final String PARSED_PATTERN_BEGIN = "<font color=\"#888888\"><tt>";
private static final String PARSED_PATTERN_END = "</tt></font>";
}

android mapview custom tile

I need to add a custom base layer to to my map view. My understanding is that the map tiles are exactly the same as the google tiles. They are static files served up in the following format: http:///tilecache///.png
For example, the http:///tilecache/6/16/26.png is the gulf coast between florida alabama and mississippi.
How do I create an overlay with the tiles?
osmdroid (as recommended above) is cool but quite huge. Some time ago I decided to use http://sourceforge.net/projects/libwlocate/ instead - it contains functionalities for showing/scrolling/zooming maps like osmdroid but you can choose to use OSM, Google Maps or Google satellite view.
An example how to use it can be found in http://libwlocate.git.sourceforge.net/git/gitweb.cgi?p=libwlocate/libwlocate;a=blob;f=master/android/LocDemo/src/com/vwp/locdemo/LocDemo.java;h=5e2134fb7d197258f5f4f6f9021d2fa9ad9f2d9a;hb=HEAD
I would recommend using osmdroid. You can extend OnlineTileSourecBase.
public class YourTileSource extends OnlineTileSourceBase implements IStyledTileSource<Integer> {
public YourTileSource (String aName, string aResourceId,
int aZoomMinLevel, int aZoomMaxLevel, int aTileSizePixels,
String aImageFilenameEnding, String aBaseUrl) {
super(aName, aResourceId, aZoomMinLevel, aZoomMaxLevel, aTileSizePixels,
aImageFilenameEnding, aBaseUrl);
}
public void setStyle(Integer style) {
// TODO Auto-generated method stub
}
public void setStyle(String style) {
// TODO Auto-generated method stub
}
public Integer getStyle() {
// TODO Auto-generated method stub
return null;
}
#Override
public String getTileURLString(MapTile aTile) {
return getBaseUrl() + "/" + aTile.getX() + "/" + aTile.getY() + "/" + aTile.getZoomLevel() + ".png";
}
}
Then add your tile source to your mapview:
TileSourceFactory.addTileSource(new YourTileSource ("YourTileSource", null, 1, 20, 256, ".png", "http:///tilecache/"));
mapView.setTileSource(TileSourceFactory.getTileSource("YourTileSource"));
Your mapView needs to be an org.osmdroid.views.MapView for that to work. OSMdroid classes should replace all of the google maps classes.
Start by downloading the osmdroid-android-3.0.8.jar file, add it to your libs folder in your project, and then add it to the project through right click > Properties > Java Build Path > Libraries > Add Jars, and then find it in the libs folder. Post more questions if you have them, I have a lot of experience with osmdroid.
For newer versions of OSMDroid, the aResourceId parameter to the constructor has been removed and the last parameter, aBaseUrl, is a String[]
public YourTileSource (String aName,
int aZoomMinLevel,
int aZoomMaxLevel,
int aTileSizePixels,
String aImageFilenameEnding,
String[] aBaseUrl) {
super(aName,
aZoomMinLevel,
aZoomMaxLevel,
aTileSizePixels,
aImageFilenameEnding,
aBaseUrl);
}

Categories

Resources