Created
March 27, 2013 04:50
-
-
Save mnive93/5251731 to your computer and use it in GitHub Desktop.
java application making use of google static map api using latitude and longitude
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package map; | |
import java.awt.Graphics; | |
import java.awt.Image; | |
import java.io.FileOutputStream; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
import java.util.*; | |
import java.sql.*; | |
import javax.swing.*; | |
/** | |
* | |
* @author lenovo | |
*/ | |
public class mapmain extends javax.swing.JFrame { | |
ImageIcon icon,icon2; | |
Connection c; | |
Statement st; | |
PreparedStatement pst; | |
ResultSet rs; | |
int Id2; | |
URL server; | |
float lon,lat; | |
HttpURLConnection connection; | |
void connect_map(String url) | |
{ | |
try | |
{ | |
String proxy = "proxy.ssn.net"; | |
String port = "8080"; | |
server = new URL(url); | |
Properties systemProperties = System.getProperties(); | |
systemProperties.setProperty("http.proxyHost",proxy); | |
systemProperties.setProperty("http.proxyPort",port); | |
connection = (HttpURLConnection)server.openConnection(); | |
connection.connect(); | |
} | |
catch(Exception e) | |
{ | |
System.out.println(e); | |
} | |
} | |
public void map_gen(int id) | |
{ | |
try | |
{ | |
pst = c.prepareStatement("select lat,lon from Restaurant where id="+id+""); | |
rs = pst.executeQuery(); | |
rs.next(); | |
lat=Float.valueOf(rs.getString(1)); | |
lon=Float.valueOf(rs.getString(2)); | |
System.out.println(lat); | |
String url = "http://maps.googleapis.com/maps/api/staticmap?&zoom=13&size=600x300&maptype=roadmap&markers=color:blue%7Clabel:S%7C"+lat+","+lon+"&sensor=true"; | |
connect_map(url); | |
System.out.print(url); | |
System.out.print("connected to url"); | |
InputStream is = connection.getInputStream(); | |
OutputStream os; | |
os = new FileOutputStream("C://Users/lenovo/Pictures/picture.jpg"); | |
byte[] b = new byte[2048]; | |
int length; | |
while ((length = is.read(b)) != -1) { | |
os.write(b, 0, length); | |
} | |
System.out.printf("hereeeee"); | |
icon = new ImageIcon("C://Users/lenovo/Pictures/picture.jpg"); | |
System.out.println("mp created!"); | |
icon2 = new ImageIcon("E:/java proj/New Folder/bg1.jpg"); | |
initComponents(); | |
} | |
catch(Exception e) | |
{ | |
System.out.println(e); | |
} | |
} | |
/** | |
* Creates new form mapmain | |
*/ | |
public mapmain() | |
{ | |
} | |
public mapmain(int Id) { | |
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> | |
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. | |
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html | |
*/ | |
//</editor-fold> | |
System.out.println("MySQL Connect Example."); | |
String url = "jdbc:mysql://localhost:3306/"; | |
String dbName = "map"; | |
String driver = "com.mysql.jdbc.Driver"; | |
String userName = "root"; | |
String password = "nive"; | |
try { | |
Class.forName(driver).newInstance(); | |
c = DriverManager.getConnection(url+dbName,userName,password); | |
System.out.println("Connected to the database"); | |
Id2=Id; | |
map_gen(Id); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
@SuppressWarnings("unchecked") | |
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents | |
private void initComponents() { | |
jLabel1 = new javax.swing.JLabel(); | |
jPanel1 = new javax.swing.JPanel() | |
{ | |
protected void paintComponent(Graphics g) | |
{ | |
g.drawImage(icon2.getImage(), 15, 5, null); | |
super.paintComponent(g); | |
} | |
} | |
; | |
String [] items = new String[20]; | |
int i=0; | |
try | |
{ | |
pst=c.prepareStatement("select name from directions"); | |
rs=pst.executeQuery(); | |
while(rs.next()) | |
{ | |
items[i]=rs.getString(1); | |
i+=1; | |
} | |
} | |
catch(Exception e) | |
{ | |
System.out.println(e); | |
} | |
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed | |
// TODO add your handling code here: | |
m3frame m = new m3frame(Id2); | |
this.setVisible(false); | |
m.setVisible(true); | |
}//GEN-LAST:event_jButton1ActionPerformed | |
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed | |
// TODO add your handling code here: | |
String s = (String)dest.getSelectedItem(); | |
System.out.println(s); | |
float lat1,lon1; | |
try | |
{ | |
pst=c.prepareStatement("select lat,lon from directions where name='"+s+"'"); | |
rs=pst.executeQuery(); | |
rs.next(); | |
lat1=Float.valueOf(rs.getString(1)); | |
lon1=Float.valueOf(rs.getString(2)); | |
String url = "http://maps.googleapis.com/maps/api/staticmap?&zoom=13&size=800x800&maptype=roadmap&path=color:0x0000ff|weight:5|"+lat1+","+lon1+"|"+lat+","+lon+"&markers=color:blue%7Clabel:S%7C"+lat1+","+lon1+"&markers=color:green%7Clabel:G%7C"+lat+","+lon+"&sensor=true"; | |
connect_map(url); | |
InputStream is = connection.getInputStream(); | |
OutputStream os; | |
os = new FileOutputStream("C://Users/lenovo/Pictures/picture2.jpg"); | |
byte[] b = new byte[2048]; | |
int length; | |
while ((length = is.read(b)) != -1) { | |
os.write(b, 0, length); | |
} | |
System.out.printf("hereeeee"); | |
icon = new ImageIcon("C://Users/lenovo/Pictures/picture2.jpg"); | |
System.out.println("mp created!"); | |
m5frame m = new m5frame(icon); | |
m.setVisible(true); | |
} | |
catch(Exception e) | |
{ | |
System.out.println(e); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment