Android从手机发射塔获取位置 [英] Android get location from cell tower

查看:115
本文介绍了Android从手机发射塔获取位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想从手机塔(Sim卡)获得位置而不需要网络.我使用了下面提到的代码,当网络可用时,它仅给我最后一个位置.

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.

我的要求是在没有WiFi或互联网的情况下获取位置.有可能吗?

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();  
    }  
}  

推荐答案

小区地理位置通过向服务器发送小区塔ID并将其映射到坐标来工作.因此,不幸的是,没有使用标准API的数据连接就无法获得坐标.

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.

这篇关于Android从手机发射塔获取位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆