1

I have two classes MainActivity.java (Main Class) & MyCurrentLoctionListener.java

I am willing to get my current geo location when I run my app (onLoad-onCreate) :

I get Hi :: null

even though I defined myLocation as public , so the MainActivity.java (Main Class) can see it.

MainActivity.java :

package com.example.test;

import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button btn1 = (Button) findViewById(R.id.make_folder);
TextView txt1 = (TextView) findViewById(R.id.textView1);

//import android.location.LocationManager;
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
MyCurrentLoctionListener locationListener = new MyCurrentLoctionListener();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,locationListener);    

txt1.setText("HI :: " + locationListener.myLocation);

btn1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {   
}           
}); //setOnClickListener    
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

MyCurrentLoctionListener.java :

package com.example.test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;

public class MyCurrentLoctionListener implements LocationListener {

public String myLocation;

@Override
public void onLocationChanged(Location location) {
location.getLatitude();
location.getLongitude();



myLocation = "Latitude = " + location.getLatitude() + " Longitude = " + location.getLongitude();

}

@Override
public void onStatusChanged(String s, int i, Bundle bundle) {

}

@Override
public void onProviderEnabled(String s) {

}

@Override
public void onProviderDisabled(String s) {

}
}

Permissions are already included in Manifest.xml

eawedat
  • 409
  • 2
  • 8
  • 17

2 Answers2

3

When you start your activity, your MyCurrentLoctionListener will not be able to get the location right away.

It would be best to pass the TextView into your MyCurrentLoctionListener class like this:

public class MyCurrentLoctionListener implements LocationListener {

public String myLocation;
private TextView mTextView;

MyCurrentLoctionListener(TextView tv) {
    this.mTextView = tv;    
}

@Override
public void onLocationChanged(Location location) {
    location.getLatitude();
    location.getLongitude();

    mTextView.setText("Latitude = " + location.getLatitude() + " Longitude = " + location.getLongitude());

}

....
chrisbjr
  • 628
  • 4
  • 10
  • thanks a lot :) it worked!! Is it possible to get a graphical map (image/photo) with defined size (WidthxHeight) of these coordinates ? like to do a function which accepts two arguments of Latitude & Longitude and save the map as an image or photo to external memory card ? – eawedat Oct 29 '13 at 17:38
  • Use Nokia's HERE Maps API for what you are trying to achieve. [link](http://developer.nokia.com/Community/Wiki/HERE_Maps_API_-_Using_the_geocoding_service) – Matt Kula Oct 30 '13 at 16:11
1

It is probably because you are using myLocation too soon. When you request for GPS data with an onLocationChanged, it might take a second to find your coordinates so you need to take that into account. Maybe change the TextView within the onLocationChanged function or something of the sort.

Matt Kula
  • 268
  • 2
  • 8