want to find Lantitude and longitude and display MAP
Hi, my project is FRIENDS AND FAMILY LOCATOR in which i neeed to find latitude and longitude of a place where i m, have developed a code if i m providing the co-ordinates manually then it is showing me the exact location but otherwise if m not giving ny constant values and wanting to change the co-ordinates at regular intervals ... co-ordinates r the same and showing map of some different location
[CODE]
public class MidletGPS extends MIDlet implements CommandListener,LocationListener
{
Form mainForm = new Form("Info GPS");
StringItem infoGPS = new StringItem("GPS status:", "unknown");
StringItem infoCoordinates = new StringItem("Coordinates:", "unknown");
Command cmdExit = new Command("EXIT", Command.EXIT, 1);
Command cmdStart = new Command("GPS data", Command.SCREEN, 1);
Command cmdDisplay = new Command("Display map", Command.SCREEN, 2);
LocationProvider lp;
Location loc;
QualifiedCoordinates qc;
double latitude = 0; //default values
double longitude = 0; //default values
public MidletGPS()
{
mainForm.append(infoGPS);
mainForm.append(infoCoordinates);
mainForm.addCommand(cmdExit);
mainForm.addCommand(cmdStart);
mainForm.addCommand(cmdDisplay);
}
private void getGPSData()
{
Display.getDisplay(this).setCurrent( mainForm);
try {
Criteria c=new Criteria();
c.setHorizontalAccuracy(1000);
c.setVerticalAccuracy(1000);
c.setPreferredPowerConsumption(Criteria.POWER_USAGE_LOW);
lp=LocationProvider.getInstance(c);
loc=lp.getLocation(60);
qc=loc.getQualifiedCoordinates();
mainForm.append("Alt: "+qc.getAltitude());
mainForm.append("Lat: "+qc.getLatitude());
mainForm.append("Long: "+qc.getLongitude());
Alert a = new Alert("DONE");
mainForm.append("DONE");
}
catch(Exception e)
{
mainForm.append("Exception: "+e);
}
}
public void startApp()
{
Display.getDisplay(this).setCurrent(mainForm);
mainForm.setCommandListener(this);
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
public void commandAction(Command c, Displayable d)
{
if (c == cmdExit)
{
//provider.setLocationListener(null, -1, -1, -1);
destroyApp(true);
notifyDestroyed();
}
else if (c == cmdStart)
{
getGPSData();
}
else if (c == cmdDisplay)
{
GoogleMaps gm = new GoogleMaps(this, Double.toString(latitude), Double.toString(longitude));
Display.getDisplay(this).setCurrent(gm);
}
}
public void locationUpdated(LocationProvider arg0, Location arg1)
{
if (arg1 != null && arg1.isValid())
{
//get the coordinates
Coordinates coordinates = arg1.getQualifiedCoordinates();
if (coordinates != null)
{
//get the latitude and longitude of the coordinates.
latitude = coordinates.getLatitude();
longitude = coordinates.getLongitude();
infoCoordinates.setText("\n" + "latitude: " + latitude + "\n" + "longitude: " + longitude);
}
else
{
//no valid coordinates
infoGPS.setText("GPS inactive");
infoCoordinates.setText("unknown");
}
}
}
public void providerStateChanged(LocationProvider arg0, int arg1)
{
if (arg1 == LocationProvider.OUT_OF_SERVICE || arg1 == LocationProvider.TEMPORARILY_UNAVAILABLE)
{
infoGPS.setText("GPS inactive");
}
}
/*public void setCurrentForm(Displayable nextForm)
{
Display.getDisplay(this).setCurrent(nextForm);
}*/
public class GoogleMaps extends Canvas implements CommandListener
{
Command cmdBack = new Command("Back", Command.EXIT, 1);
Command cmdRefresh = new Command("Refresh", Command.SCREEN, 1);
//reference to the parent MIDlet
MidletGPS midGPS;
int zoom = 2;
double latitude = 0;
double longitude = 0 ;
double altitude = 0;
HttpConnection connection = null;
public GoogleMaps(MidletGPS mGPS, String Lat, String Longit)
{
//only for testing
//latitude = "18.961968";
//longitude = "72.835493";
//latitude = Lat;
//longitude = Longit;
latitude = (qc.getLatitude());
longitude = (qc.getLongitude());
altitude = (qc.getAltitude());
midGPS = mGPS;
this.addCommand(cmdBack);
this.addCommand(cmdRefresh);
this.setCommandListener(this);
}
public void commandAction(Command c, Displayable d)
{
if (c == cmdBack)
{
Display.getDisplay(midGPS).setCurrent(midGPS.mainForm);
}
if (c == cmdRefresh)
{
this.repaint();
}
}
public HttpConnection conGoogleMap()
{
//get the width and the height of the screen
final int width = getWidth();
final int height = getHeight();
new Thread()
{
public void run()
{
//the query string for the Google service
String url = "http://maps.google.com/maps/api/staticmap?center=";
url += latitude + "," + longitude;
url += "&zoom=" + String.valueOf(zoom);
url += "&size=" + width + "x" + height + "&sensor=true";
try
{
connection = (HttpConnection) Connector.open(url);
connection.setRequestMethod(HttpConnection.GET);
}
catch (Exception ex)
{
ex.printStackTrace();
}
/*finally
{
try
{
if (connection != null)
{
connection.close();
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}*/
}
}.start();
return connection;
}
public Image getGoogleMap()
{
InputStream inputStream = null;
HttpConnection connection2 = null;
try
{
connection2 = conGoogleMap();
inputStream = connection2.openInputStream();
Image map = Image.createImage(inputStream);
//another solution for creating the Image
//ByteArrayOutputStream byteArray = new ByteArrayOutputStream();//
//get the image byte by byte
//int c;
//while ((c = inputStream.read()) != -1)
//{
//byteArray.write(c);//
//}
//byte[] buffer = byteArray.toByteArray();
//byteArray.close();//
//create an Image object
//logo = Image.createImage(buffer, 0, buffer.length);
return map;
}
catch (Exception ex)
{
ex.printStackTrace();
}
/*finally
{
try
{
if (inputStream != null)
{
inputStream.close();
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}*/
return null;
}
//override paint handler
protected void paint(Graphics g)
{
//get the image
Image map = getGoogleMap();
if(map!=null)
{
//draw the image on the canvas
g.drawImage(map, 0, 0, Graphics.LEFT | Graphics.TOP);
}
else
{
g.setColor(255,0,0);
g.drawString("No map available", 20, 20, 0);
}
}
protected void keyPressed(int keyCode)
{
if (((char) keyCode) == '1')
{
zoom--;
}
if (((char) keyCode) == '3')
{
zoom++;
}
//if you want to move the map in all directions
double offset = 0.02;
if (getGameAction(keyCode) == 5)
{
double lon = (longitude);
lon += offset / zoom;
longitude = lon;
}
if (getGameAction(keyCode) == 2)
{
double lon = (longitude);
lon -= offset / zoom;
longitude = (lon);
}
if (getGameAction(keyCode) == 4)
{
double lat = (latitude);
lat += offset / zoom;
latitude = (lat);
}
if (getGameAction(keyCode) == 6)
{
double lat = (latitude);
lat -= offset / zoom;
latitude = (lat);
}
//call the paint event
this.repaint();
}
}
}[/CODE]
Plzz help me out... m stuck at this particular point!!!
plzz do help me asap!!!
Thanks
Vinita!!!
Re: want to find Lantitude and longitude and display MAP
I'm not understanding the actual problem, so could you explain in details on:
1. What you are trying to do
2. How you are trying to do it,
3. What would you expect to happen
4. what appears to work
5. What appears not to work.
Re: want to find Lantitude and longitude and display MAP
As i want to locate friends where they are i.e. latitude and longitude of the location where friend is. M using GPS data to find the co-ordinates.
the code which i have posted in that if i am giving the co-ordinates manually it is showing a proper map.
but actually i want the latitude and longitude to be updated at regular intervals as per my location changes. with dis code m getting some incorrect co-ordinates
and accordng to that co-ordinates the map which is shown is also not the correct one. and this co-ordinates are not changing at all.
Re: want to find Lantitude and longitude and display MAP
Basically you would need to get the location from your friends GPS, and then have a mechanism on sending the information into your own device to show, so how are you achieving this ?
For the updating of your own location, you would basically not need any additional logic, you could simply use the position values as they are given to you by the API, and indeed it should tell you when you are moving automatically.
Re: want to find Lantitude and longitude and display MAP
It is like i would decide certain co-ordinates which will be a margin. whenever my friend is within that particular margin(co-ordinates) it will give me an alert!!!
but dont xactly hav idea abt how to get the location from my friends GPS, and then have a mechanism on sending the information into my own device to show!!
i was just trying Basic GPS to try how actually GPS works....
can u tell wat actually will i hav to do for this,.... plzz sir i need it!!!
Thanks
Vinita!!!
Re: want to find Lantitude and longitude and display MAP
Basically, the alarm is easy, if I remember right the GeoCoordinate class does implement function on checking distance to other GeoCoordinate, so you can use them directly.
For the sending the coordinates problem, I suppose you need to decide yourself how you would want to do that. In general you would need to get something running in their device, which would get the location and then let your other devices to know about it.
I suppose one way would be to have your own server which would take location updates from clients, as well as give the values to other clients when asked for.
All and all, you do need to design that yourself, and there are not too much that could be autogenerated etc.
Re: want to find Lantitude and longitude and display MAP
Hi, Sir m still not gettng how can i retrieve Gps co-ordinates automatically...... i have goggled a lot but hav found code consisting of Qualifiedcoordinates whch provides me with some inbuilt values. In my project i need to first track my own location but i dont know how is that possible,,......
m stuck... i need ur help urgently!!!
THANKS in advance
Vinita
Re: want to find Lantitude and longitude and display MAP
You could try checking our wiki, f.ex. : [url]http://www.developer.nokia.com/Community/Wiki/Examples_for_Maps_API_for_Java_ME[/url] and the MyLocationMap should work for you