Displaying map from Open Street Map (OSM) inside google map api
In many countries google’s map data is inadequate; it lacks street level data in many countries.
In these cases Open Street Map is a good solution. Most of the applications written using google map api use google map data for displaying map. In this cases one should find a solution to integrate OSM inside google map api.
After some RnD on google map api and OSM I found out a solution to this. Google map api has a nice provision to add custom map types. And I used this feature to implement the solution. I added a custom map type in my google map and fetch the desired tiles from osm server. Here is the JavaScript code snippet:
var map = new GMap(document.getElementById(“map”));
map.addControl(new GScaleControl());
map.addControl(new GLargeMapControl());
map.addControl(new GMapTypeControl());
//Custom function for fetchng tiles from OSM server
CustomGetTileUrl=function(a,b){
return ‘http://a.tile.openstreetmap.org/’+b+’/’+a.x+’/’+a.y+’.png’;
}
var copyright = new GCopyright(1,
new GLatLngBounds(new GLatLng(53.8136257,-3.0981445),
new GLatLng(53.8654855,-2.9663944) ),
17, “”);
var copyrightCollection = new GCopyrightCollection(”);
copyrightCollection.addCopyright(copyright);
var tilelayers = [new GTileLayer(copyrightCollection,1,17)];
tilelayers[0].getTileUrl = CustomGetTileUrl;
var osmmap = new GMapType(tilelayers, G_SATELLITE_MAP.getProjection(), “O.S.M.”);
map.addMapType(osmmap);
map.setCenter(new GLatLng(34.6891,33.0182), 14, osmmap);
This will create an extra map type labeled “O. S.M.” besides the standard map types of google map api.
Load Balancing Tomcat with Apache
Recently I was working on server scalability issue of one of my projects. So I had to went through some RnD on load balancing and finally was able to configure it. I used tomcat as servlet container and apache as load balancer. I used mod_proxy and mod_proxy_balancer module of apache for load balancing between two tomcat instances running on port 8080 and 3333.
Here is the details:
Server used:
Tomcat 6.0.16 as servlet container x 2
Apache 2.2.6 as load balancer x 1
Testing Tool:
Jakarta JMeter 2.6.1
Apache Configuration:
Here, I created a cluster named mycluster using 2 tomcat server and sticksession named as JSESSIONID. My test application myapp was running on both of the tomcat server.
I used loadfactor 50 on both of the tomcat server (meaning both of them will be sharing equal load). I
1. Enable mod_proxy: In httpd.conf enable/add
LoadModule proxy_module modules/mod_proxy.so
2. Enable mod_proxy_balancer: In httpd.conf
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
3. Configure the load balancer on the cluster and balancer manager:
In httpd.conf add these lines:
ProxyPass /myapp balancer://mycluster stickysession=JSESSIONID
<Proxy balancer://mycluster>
BalancerMember http://localhost:8080/myapp route=tomcat1 loadfactor=50
BalancerMember http://localhost:3333/myapp route=tomcat2 loadfactor=50
</Proxy>
<Location /balancer-manager>
SetHandler balancer-manager
</Location>
After restarting the server and hitting http://localhost/balancer-manager in a browser it showed me the configurations of the cluster.
Tomcat Configuration:
1. In server.xml file of tomcat running on 8080 port I configured the jvm route as tomcat1 as :
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">
By editing the engine tag.
2. In server.xml file of tomcat running on 3333 port I configured the jvm route as tomcat1 the same way:
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2">
The testing application:
I used a single servlet for the test application myapp.
Here is the code snippet of the servlet named myservlet:
ServletContext sc = this.getServletContext();
String servername = sc.getInitParameter("
servername
"
);
out.print("
This is from ::
"
+ servername);
In web.xml of the application I added a context parameter named servername which value was server1 while deploying into tomcat1(on port 8080) and server2 on tomcat2(running on 3333 port).
Testing:
With JMeter I created a script for hiitting the url http://localhost/myapp/myservlet using 400 concurrent thread in 20 loops to see the effect.
Links of toturials for mysql performance tuning
Here is some tutorials’ links for mysql performance tuning:
Sending array in GET method in Java Servlet
Suppose I want to send an array of names from any http client in get method to a servlet like,
name[0] = “sihan”
name[1] = “rasel”
name[2] = “siam”
name[3] = “shamim”
All I have to do build the query string like this :
http://server_url:server_port/servletname?name=sihan&name=rasel&name=siam&name=shamim
And in the servlet we can get the array like this:
String[] names = request.getParameterValues("name");
Then ‘names‘ array will contain all the four values passed in ‘name‘ parameter.
MMAPI workarounds in different mobile devices : part 1
I have been working with mmapi in j2me for the last few days and found some interesting things on different devices.
Set: Nokia-6300
1. Locator string for capturing should be different for capturing image and video
For image it should be,
“capture://image”
For video it should be,
“capture://video”
2. Shows device(camera) unavailable when battery charge is low.
3. png encoding in getSnapShot() method is not supported. Using null as parameter works fine.
Set: Sony Ericsson W850i
1. Problem: Got black screen while capturing more than one time(in canvas).
Solution: Probably caused by paint overwrite problem in this device. Used form display to show camera and it solved the problem.
2. Locator string for capturing is “capture://audio_video” for capturing image and video
3. “png” encoding in getSnapShot() method is not supported. Also custom height, width parameter does not work. Using null as parameter works fine.
4. Video rotates 90 degree counter clockwise after capturing: yet to find a solution.
Note:
1. null in the getSnapShot() method means default parameter of the set which is “encoding=png” and “dimension=480×640” in case of SE 850i
2. In Other sets of Nokia N series(95,93,90,82,76,73,70) the locator string “capture://video” works fine; also png encoding and custom dimensions works fine.
Here is some important system properties to check while working with MMAPI:
- “microedition.amms.version”
- “microedition.media.version”
- “supports.mixing”
- “supports.audio.capture”
- “supports.video.capture”
- “supports.recording”
- “audio.encodings”
- “video.encodings”
- “video.snapshot.encodings”
- “streamable.contents”
Encoding URL in j2me
In j2se we can use built-in URLEncoder for encoding urls. But j2me has no built-in URLEncoder support. We need to implement our own method to encode url.
Here is a sample code for such an URLEncoder:
public class URLEncoder {
public static String encode(String s, String enc) throws IOException {
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
DataOutputStream dOut = new DataOutputStream(bOut);
StringBuffer ret = new StringBuffer(); //return value
dOut.writeUTF(s);
ByteArrayInputStream bIn = new ByteArrayInputStream(bOut.toByteArray());
bIn.read();
bIn.read();
int c = bIn.read();
while (c >= 0) {
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '.' || c == '-' || c == '*' || c == '_') {
ret.append((char) c);
} else if (c == ' ') {
ret.append('+');
} else {
if (c < 128 ) {
appendHex(c, ret);
} else if (c < 224) {
appendHex(c, ret);
appendHex(bIn.read(), ret);
} else if (c < 240) {
appendHex(c, ret);
appendHex(bIn.read(), ret);
appendHex(bIn.read(), ret);
}
}
c = bIn.read();
}
return ret.toString();
}
private static void appendHex(int arg0, StringBuffer buff) {
buff.append('%');
if (arg0 < 16) {
buff.append('0');
}
buff.append(Integer.toHexString(arg0));
}
}
Note:
Encode each parameter name and its value with the encoder not the whole URL.
Like, to encode the parameter “user name=j2me & test|” use:
URLEncoder.encode(“user name”) as parameter and URLEncoder.encode(“j2me & test|”) and thus build the whole query string.
Jad/Jar installing problem from server on Mobile device
I had to face a strange problem while downloading jad/jar from my server into mobile device. It was not installing by downloading from the server but I could install and run the same jad/jar transferring by bluetooth device. After some googling I came to know that the problem was in my server mime type settings for .jad extension and I could solve this thing. I had no mime type defined in my server for the extension and mobile browser could not recognize the .jad file.
So, I added the following line in my Apache server’s ‘mime.types’ configuaration file:
text/vnd.sun.j2me.app-descriptor jad
And it worked like a charm.
For apache tomcat server it should be added in the “web.xml” file like this:
<mime-mapping>
<extension>jad</extension>
<mime-type>text/vnd.sun.j2me.app-descriptor</mime-type>
</mime-mapping>
Capturing website thumbnail
Some months before I had to capture and show the thumbnail of website dynamically and I found an excellent tool named ‘IECapt’. It is a sourceforge project.
“IECapt is a simple command line utility that uses the Internet Explorer rendering engine to render a web page into a GIF/JPEG/BMP/PNG image file” (Quoted from sourceforge.net).
It is a written in C++, licensed under GPL and runs in 32 Bit Windows XP/NT/2000. It can be found here.
Sample command:
From windows command line type the following command:
IECapt.exe https://sihantech.wordpress.com thumb.png
This will create an image ‘thumb.png’ in local disk by capturing a screenshot of the given url.
Now we can execute this by PHP using exec() function or by java using Runtime.getRuntime().exec() method.