Android get location from cell tower - android

Hi I want to get location from cell tower(Sim card) without internet. I had used below mentioned code it's give me only last location when network available.
my requirement is to get location without WiFi or internet. can it's possible?
package com.example.gpscelltower;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
public class CellTowerActivity1 extends Activity {
private static final String TAG = "Cell";
private static LocationManager locationManager;
private static LocationListener locationListener;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cell_tower);
locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
String providerName=LocationManager.NETWORK_PROVIDER;
Location location=locationManager.getLastKnownLocation(providerName);
updateWithLocation(location);
//Cell-ID and WiFi location updates.
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, locationListener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, locationListener);
locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
Log.d("TAG", "locationListner");
updateWithLocation(location);
String Text = "My current location is: " + "Latitude = "+ location.getLatitude() + "Longitude = " + location.getLongitude();
Toast.makeText(getApplicationContext(), Text, Toast.LENGTH_LONG).show();
Log.d("TAG", "Starting..");
}
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d("TAG", "onSatatus");
}
public void onProviderEnabled(String provider) {
Log.d("TAG", "onProviderEnable");
}
public void onProviderDisabled(String provider) {
Log.d("TAG", "onProviderDisble");
}
};
}
private void updateWithLocation(Location location) {
Log.d("TAG", "UpdateWithLocation");
String displayString;
if(location!=null){
Double lat=location.getLatitude();
Double lng=location.getLongitude();
Long calTime=location.getTime();
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss.SSS");
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(calTime);
String time=formatter.format(calendar.getTime()).toString();
displayString="The Lat: "+lat.toString()+" \nAnd Long: "+lng.toString()+"\nThe time of Calculation: "+time;
}else{
displayString="No details Found";
}
Toast.makeText(CellTowerActivity1.this, ""+displayString, Toast.LENGTH_LONG).show();
}
}

Cell geolocation works through sending Cell towers IDs to server which maps it to coordinates. So, unfortunately it's not posssible to get your coordinates w/o data connection with standard APIs.

Related

Android: GPS coordinates working on Emulator but not on the phone

I am an android beginner and I need your help! I am building a very simple running app and would like to have gather information about the speed. For this I am trying to create a very simple GPS class in order to use the getspeed() method.
I have created a little test app to ensure that my code is sound. In this app I am using getLatitude() and getLongitude() too but these will not be useful for my final code - I only need the speed really.
So far, I have managed to successfully test the class on the emulator but not on the phone. I can see the coordinates being populated on the screen when using the DDMS coordinates (getspeed() remained 0 but I know I cant test that on the emulator). When I try the code on my phone I have no response at all - despite waiting for the GPS to "warm up" (I also see that he GPS works when I move to GoogleMaps).
The whole things is driving me crazy, I am not sure what is happening with the onLocationChanged() method so appreciate any help you can give me.
Thanks!
The code is as per below:
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
TextView speedText1, speedText2, speedText3;
double speed = 0;
double latitude = 0;
double longitude = 0;
String speed1;
String latitude1;
String longitude1;
boolean gps_enabled;
Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
gps_enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
Toast.makeText(getBaseContext(), "location not null" , Toast.LENGTH_SHORT).show();
speedText1 = (TextView) findViewById(R.id.textView1);
speedText2 = (TextView) findViewById(R.id.textView2);
speedText3 = (TextView) findViewById(R.id.textView3);
speed = location.getSpeed();
speed1 = Double.toString(speed);
speedText1.setText(speed1);
latitude = location.getLatitude();
latitude1 = Double.toString(latitude);
speedText2.setText(latitude1);
longitude = location.getLongitude();
longitude1 = Double.toString(longitude);
speedText3.setText(longitude1);
Toast.makeText(getBaseContext(), latitude1 + longitude1 + speed , Toast.LENGTH_SHORT).show();
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
public void onProviderEnabled(String provider) {
}
public void onProviderDisabled(String provider) {
} };
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
}
You are defining the LocationListener inside the onCreate() method, so you will not get location updates once that method finishes. You should try something like this:
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements LocationListener {
TextView speedText1, speedText2, speedText3;
double speed = 0;
double latitude = 0;
double longitude = 0;
String speed1;
String latitude1;
String longitude1;
boolean gps_enabled;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 0.0f, this);
gps_enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
Toast.makeText(this, "location not null" , Toast.LENGTH_SHORT).show();
speedText1 = (TextView) findViewById(R.id.textView1);
speedText2 = (TextView) findViewById(R.id.textView2);
speedText3 = (TextView) findViewById(R.id.textView3);
}
#Override
public void onLocationChanged(Location location) {
speed = location.getSpeed();
speed1 = Double.toString(speed);
speedText1.setText(speed1);
latitude = location.getLatitude();
latitude1 = Double.toString(latitude);
speedText2.setText(latitude1);
longitude = location.getLongitude();
longitude1 = Double.toString(longitude);
speedText3.setText(longitude1);
Toast.makeText(this, latitude1 + longitude1 + speed, Toast.LENGTH_SHORT).show();
}
}
}

Network location returned is wrong

I want to get the location of the user using network. This is my code
package com.example.locationtest;
import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.util.Log;
public class WhereAmI extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LocationManager locationManager;
String context = Context.LOCATION_SERVICE;
locationManager = (LocationManager) getSystemService(context);
Criteria crta = new Criteria();
crta.setAccuracy(Criteria.ACCURACY_FINE);
crta.setAltitudeRequired(false);
crta.setBearingRequired(false);
crta.setCostAllowed(true);
crta.setPowerRequirement(Criteria.POWER_LOW);
String provider = locationManager.getBestProvider(crta, true);
// String provider = LocationManager.GPS_PROVIDER;
Location location = locationManager.getLastKnownLocation(provider);
updateWithNewLocation(location);
locationManager.requestLocationUpdates(provider, 1000, 0,
locationListener);
}
private final LocationListener locationListener = new LocationListener() {
#Override
public void onLocationChanged(Location location) {
updateWithNewLocation(location);
}
#Override
public void onProviderDisabled(String provider) {
updateWithNewLocation(null);
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
private void updateWithNewLocation(Location location) {
String timeStamp = (String) DateFormat.format("hh:mm:ss",
new java.util.Date());
String locationString = location.getLatitude() + ", "
+ location.getLongitude();
Log.i("Main", "Time = " + timeStamp + " Location = " + locationString);
}
}
This code works with my nexus 4, but if I use it with my Galaxy Gio the location is off by 30 Miles..
network location is based on the nearest Cell tower which the phone is connected to. It could be possible that your Nexus4 is connected to one Cell tower and the Galaxy Gio connected to another cell tower that could be like a bit far from where the Device is. To get an accurate fix, use GPS_PROVIDER. I had this issue before when using a NETWORK_PROVIDER. Got rid of it when I started using the GPS_PROVIDER which gave me accurate fixes

Getting longitude and latitude works in emulator but not on device?

I've been trying to work GPS coordinates but it's been a lot tougher than I thought. After a few hours of trial and error, I've managed to output the latitude and longitude (mocked) for my emulator. Below are the 2 ways I've done it:
First way:
import java.io.IOException;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;
import android.app.Activity;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String text = "";
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location != null) {
showMyAddress(location);
}
final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
showMyAddress(location);
}
public void onProviderDisabled(String arg0) {
}
public void onProviderEnabled(String arg0) {
}
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
}
};
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30000, 10, locationListener);
double longitude = location.getLongitude();
double latitude = location.getLatitude();
Log.i(TAG, "longitude: " + longitude);
Log.i(TAG, "latitude: " + latitude);
text = "longitude: " + longitude + ", " + "latitude: " + latitude;
TextView textView = (TextView) findViewById(R.id.txtvwMain);
textView.setText(text);
}
private void showMyAddress(Location location) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
Geocoder myLocation = new Geocoder(getApplicationContext(), Locale.getDefault());
List<Address> myList;
try {
myList = myLocation.getFromLocation(latitude, longitude, 1);
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
Second way:
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class MainActivity extends Activity implements LocationListener
{
private LocationManager lm;
private TextView tv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.txtvwMain);
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 1, this);
Location location = lm.getLastKnownLocation(lm.GPS_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();
tv.setText("latitude="+latitude+", longitude="+longitude);
}
#Override
public void onLocationChanged(Location arg0) {
String lat = String.valueOf(arg0.getLatitude());
String lon = String.valueOf(arg0.getLongitude());
Log.e("GPS", "location changed: lat="+lat+", lon="+lon);
tv.setText("lat="+lat+", lon="+lon);
}
public void onProviderDisabled(String arg0) {
Log.e("GPS", "provider disabled " + arg0);
}
public void onProviderEnabled(String arg0) {
Log.e("GPS", "provider enabled " + arg0);
}
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
Log.e("GPS", "status changed to " + arg0 + " [" + arg1 + "]");
}
}
Both ways above work and print out the mock latitude and longitude onto a TextView in the emulator (running Android 4.2.2). However, when I upload the .apk file onto my device (tablet running Android 4.0.4), it just crashes. I don't know what's wrong because I can't see any error messages. What is the source of the problem and how may I go about solving it? Thanks.

how to get postal code using reverse geocoding in android

I am new to Andriod development..i try to get postal code and city name from reverse geocoding using below code.it finding latitude, longitude value fine but cant able to get cityname and postal code.
package com.example.code;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import android.app.Activity;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class LbsGeocodingActivity extends Activity {
private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // in Milliseconds
protected LocationManager locationManager;
protected Button retrieveLocationButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
retrieveLocationButton = (Button) findViewById(R.id.retrieve_location_button);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MINIMUM_TIME_BETWEEN_UPDATES,
MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
new MyLocationListener()
);
retrieveLocationButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
showCurrentLocation();
}
});
}
protected void showCurrentLocation() {
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Geocoder gcoder=new Geocoder(this, Locale.ENGLISH);
List<Address> Data = null;
double latitude;
double longitude;
if (location != null) {
latitude=location.getLatitude();
longitude=location.getLongitude();
try {
Data=gcoder.getFromLocation(latitude, longitude, 1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String cityname=Data.get(0).getLocality();
String postalcode=Data.get(0).getPostalCode();
String message=String.format("City Name \n PostalCode \n", Data.get(0).getLocality(),Data.get(0).getPostalCode());
Toast.makeText(LbsGeocodingActivity.this,message ,
Toast.LENGTH_LONG).show();
}
}
private class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
String message = String.format(
"New Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude()
);
Toast.makeText(LbsGeocodingActivity.this, message, Toast.LENGTH_LONG).show();
}
public void onStatusChanged(String s, int i, Bundle b) {
Toast.makeText(LbsGeocodingActivity.this, "Provider status changed",
Toast.LENGTH_LONG).show();
}
public void onProviderDisabled(String s) {
Toast.makeText(LbsGeocodingActivity.this,
"Provider disabled by the user. GPS turned off",
Toast.LENGTH_LONG).show();
}
public void onProviderEnabled(String s) {
Toast.makeText(LbsGeocodingActivity.this,
"Provider enabled by the user. GPS turned on",
Toast.LENGTH_LONG).show(); }
}
}
It looks like your code for finding the city and zipcode is right, but your code for formatting the string for your toast is wrong. It does not put the values in there.
It should look like this:
String message=String.format("City Name %s \n PostalCode %d \n", Data.get(0).getLocality(),Data.get(0).getPostalCode());
The %s represents a string and the %s represents a decimal number (integer).

Finding current Location when the application is running in background is not working

I want to find out current location when the Application is running in background
I Have below code for finding current location in background running services. But its not working.
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
public class MyService extends Service {
private static LocationManager mlocManager;
private static final String TAG = "MyService";
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
Log.d(TAG, "onCreate");
}
#Override
public void onDestroy() {
Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
}
#Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "start My Service Started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
//Location newloc= new Location();
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
Toast.makeText(this, "hiiiits created", Toast.LENGTH_LONG).show();
}
/* Class My Location Listener */
public class MyLocationListener implements LocationListener
{
#Override
public void onLocationChanged(Location loc)
{
Log.d(TAG, "onlocationchnage");
loc.getLatitude();
loc.getLongitude();
String Text ="My current location is: " +
"Latitud = " + loc.getLatitude() +
"Longitud = " + loc.getLongitude();
Toast.makeText( getApplicationContext(),
Text,
Toast.LENGTH_SHORT).show();
}
#Override
public void onProviderDisabled(String provider)
{
Toast.makeText( getApplicationContext(),
"Gps Disabled",
Toast.LENGTH_SHORT ).show();
}
#Override
public void onProviderEnabled(String provider)
{
Toast.makeText( getApplicationContext(),
"Gps Enabled",
Toast.LENGTH_SHORT).show();
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
}/* End of Class MyLocationListener */
}
I think you need Current Latitude and Longitude values using GPS..
follow this link.. Click link
I already tried it and test it in android mobile phone.. Its working.
Note :
emulator we are not get the Curent GPS location.. You must and should install your application in phone..
First you ON the GPS in mobile phone after try it, it will work.
have a nice day.

Categories

Resources