2013-08-31

Java Display Google Maps

This time i want share tips simplest maps showing in java swing,To display of maps in swing this we can use ireport that provides a facility map,Netbeans as Editor and Mysql as a data storage. view video tutorials on youtube

Start it,Here is the steps..

1. to do is make table at mysql to store Id,Location_Name,Latitude and Longitude.

- Login to Mysql Database

- Create new database or Entered one database in mysql (in this example i use the database 'test')
         USE test;

- Create new Table and named 'locations'
        CREATE TABLE `locations` (
        `Id` int(11) NOT NULL AUTO_INCREMENT,
        `Location_Name` varchar(200) DEFAULT NULL,
       `Latitude` float(10,5) NOT NULL,
   `Longitude` float(10,5) NOT NULL,
        PRIMARY KEY (`Id`));

- Insert row data into table 'locations'
        INSERT INTO locations VALUES
(null,'Cirebon city square','-6.70949','108.55924'),
(null,'The heritage of "Sunan Gunungjati" Cirebon','-6.67388','108.54136'),
(null,'The great mosque "Sang Cipta Rasa" Cirebon','-6.72552','108.56992');




2. Connect ireport with a database mysql and designing map with ireport






3. Create a new project MapsOnSwing on netbeans ide , in this example I'm using netbeans 7.3.
- Add the required libraries to the project lib:
  mysql-connector-java-5.1.25-bin.jar or another version
  jasperreports-4.5.0.jar or another version , adapt with the version of ireport
  groovy-all-1.5.5.jar
  commons-logging-1.1.jar
  commons-digester-1.7.jar
  commons-collections-3.2.1.jar
  commons-beanutils-1.8.2.jar




4. After the project is created, then the package will be formed mapsonswing , in this package there is already a class MapsOnSwing.java





5.  In mapsonswing package create new class MainFrame extend to JFrame,MainFrame design like the picture below :

Add desktoppane, combobox to zoom location,ComboBox Id_Location and Button to show Map

Add item to ComboBox Zoom : 15 , 16 , 17 , 18
Add item to ComboBox Id_Location : 1 , 2 , 3 or it could also take id_location from table location
Add Button to show Map



6.  Edit mapsonswing.java like this




7. In mapsonswing package create new Class IFrame extends JInternalFrame, add two parameter in constructor IFrame (int zoom,int id).




8. Make command buttons, when in clicks will feature internalframe that contains map


9.Build and run project , the result will be like this


finished , see the video on youtube

4 comments:

  1. Anonymous9:49 AM

    Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.Double cannot be cast to java.lang.Float
    at net.sf.jasperreports.components.map.fill.MapFillComponent.evaluateMap(MapFillComponent.java:93)
    at net.sf.jasperreports.components.map.fill.MapFillComponent.evaluate(MapFillComponent.java:87)
    at net.sf.jasperreports.engine.fill.JRFillComponentElement.evaluate(JRFillComponentElement.java:88)
    at net.sf.jasperreports.engine.fill.JRFillElementContainer.evaluate(JRFillElementContainer.java:259)
    at net.sf.jasperreports.engine.fill.JRFillBand.evaluate(JRFillBand.java:459)
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillColumnBand(JRVerticalFiller.java:2044)
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillDetail(JRVerticalFiller.java:778)
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportStart(JRVerticalFiller.java:288)
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:151)
    at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:909)
    at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:822)
    at net.sf.jasperreports.engine.fill.JRFiller.fill(JRFiller.java:61)
    at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:446)
    at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:849)
    at mapsonswing.IFrame.(IFrame.java:52)
    at mapsonswing.MainFrame.showButtonActionPerformed(MainFrame.java:112)
    at mapsonswing.MainFrame.access$100(MainFrame.java:12)
    at mapsonswing.MainFrame$2.actionPerformed(MainFrame.java:60)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6525)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6290)
    at java.awt.Container.processEvent(Container.java:2234)
    at java.awt.Component.dispatchEventImpl(Component.java:4881)
    at java.awt.Container.dispatchEventImpl(Container.java:2292)
    at java.awt.Component.dispatchEvent(Component.java:4703)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
    at java.awt.Container.dispatchEventImpl(Container.java:2278)
    at java.awt.Window.dispatchEventImpl(Window.java:2739)
    at java.awt.Component.dispatchEvent(Component.java:4703)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
    at java.awt.EventQueue.access$400(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:697)
    at java.awt.EventQueue$3.run(EventQueue.java:691)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:719)
    at java.awt.EventQueue$4.run(EventQueue.java:717)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

    ReplyDelete
    Replies
    1. you need to change data type for your latitude and longitude from double to float in the database

      Delete
  2. This comment has been removed by the author.

    ReplyDelete
  3. please help l m getting a Query Error after inserting the fields on number 4 when linking iReport to database.

    Java Heap Space.

    Query error


    Message:
    net.sf.jasperreports.engine.JRException: Java heap space
    Level:
    SEVERE
    Stack Trace:
    Java heap space
    com.jaspersoft.ireport.designer.data.fieldsproviders.SQLFieldsProvider.getFields(SQLFieldsProvider.java:184)
    com.jaspersoft.ireport.designer.connection.JDBCConnection.readFields(JDBCConnection.java:472)
    com.jaspersoft.ireport.designer.wizards.ConnectionSelectionWizardPanel.validate(ConnectionSelectionWizardPanel.java:146)
    org.openide.WizardDescriptor$11.run(WizardDescriptor.java:1458)
    org.openide.util.RequestProcessor$Task.run(RequestProcessor.java:1443)
    org.netbeans.modules.openide.util.GlobalLookup.execute(GlobalLookup.java:68)
    org.openide.util.lookup.Lookups.executeWith(Lookups.java:303)
    org.openide.util.RequestProcessor$Processor.run(RequestProcessor.java:2058)

    ReplyDelete