我怎么能getCurrent位置,也显示城市名 [英] how can i getCurrent location and display also city name

查看:263
本文介绍了我怎么能getCurrent位置,也显示城市名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新android系统中创建的当前位置,并显示其城市名称

i am new in android create current location and display its city name

我不知道关于code所以举一些例子说明创建当前位置,也显示城市名称thnks提前

i Have No idea About code so give some example to create current location and display also city name thnks in advance

推荐答案

使用此code从纬度经度地名 STRONG>

Use this code to get location name from latitude and longitude:

Geocoder geocoder;
        List<Address> addresses;
        geocoder = new Geocoder(context, Locale.getDefault());
            try {
                addresses = geocoder.getFromLocation(lat, lng, 1);
                if(addresses.size()>0)
                {
                String cityName = addresses.get(0).getAddressLine(0);
                 String stateName = addresses.get(0).getAddressLine(1);
                 //Toast.makeText(getApplicationContext(),stateName , 1).show();
                 String countryName = addresses.get(0).getAddressLine(2);
                 }
            }catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

用于获取当前位置使用以下步骤的纬度和经度:

for getting latitude and longitude of current location use following steps:


  • 在您的项目名称创建新类它GPSTracker.java

  • 复制和粘贴低于code到它

  • Create new class in your project name it GPSTracker.java
  • copy and paste code below into it

公共类GPSTracker扩展服务实现LocationListener的{

public class GPSTracker extends Service implements LocationListener {

private final Context mContext;

// flag for GPS status
boolean isGPSEnabled = false;

// flag for network status
boolean isNetworkEnabled = false;

// flag for GPS status
boolean canGetLocation = false;

 Location location; // location
  double latitude=0;// latitude
   double longitude=0; // longitude

// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters

// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute

// Declaring a Location Manager
protected LocationManager locationManager;

public GPSTracker(Context context) {
    this.mContext = context;
    getLocation();
}

public Location getLocation() {
    try {
        locationManager = (LocationManager) mContext
                .getSystemService(LOCATION_SERVICE);

        // getting GPS status
        isGPSEnabled = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);

        // getting network status
        isNetworkEnabled = locationManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (!isGPSEnabled && !isNetworkEnabled) {
            // no network provider is enabled
        } else {
            this.canGetLocation = true;
            // First get location from Network Provider
            if (isNetworkEnabled) {
                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                Log.d("Network", "Network");
                if (locationManager != null) {
                    location = locationManager
                            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    Log.v("location ",""+location);


                    if (location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }
                }
            }
            // if GPS Enabled get lat/long using GPS Services
            if (isGPSEnabled) {
                if (location == null) {
                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("GPS Enabled", "GPS Enabled");
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        Log.v("location ",""+location);


                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return location;
}

/**
 * Stop using GPS listener
 * Calling this function will stop using GPS in your app
 * */
public void stopUsingGPS(){
    if(locationManager != null){
        locationManager.removeUpdates(GPSTracker.this);
    }      
}

/**
 * Function to get latitude
 * */
public  double getLatitude(){
    if(location != null){
        latitude = location.getLatitude();
    }

    // return latitude
    return latitude;
}

/**
 * Function to get longitude
 * */
public  double getLongitude(){
    if(location != null){
        longitude = location.getLongitude();
    }

    // return longitude
    return longitude;
}

/**
 * Function to check GPS/wifi enabled
 * @return boolean
 * */
public boolean canGetLocation() {
    return this.canGetLocation;
}

@覆盖
公共无效onLocationChanged(地点){
    // TODO自动生成方法存根

@Override public void onLocationChanged(Location location) { // TODO Auto-generated method stub

}

@覆盖
公共无效onProviderDisabled(字符串提供商){
    // TODO自动生成方法存根

@Override public void onProviderDisabled(String provider) { // TODO Auto-generated method stub

}

@覆盖
公共无效onProviderEnabled(字符串提供商){
    // TODO自动生成方法存根

@Override public void onProviderEnabled(String provider) { // TODO Auto-generated method stub

}

@覆盖
公共无效onStatusChanged(字符串提供商,INT地位,捆绑演员){
    // TODO自动生成方法存根

@Override public void onStatusChanged(String provider, int status, Bundle extras) { // TODO Auto-generated method stub

}

@覆盖
公众的IBinder onBind(意向为arg0){
    // TODO自动生成方法存根
    返回null;
}
}

@Override public IBinder onBind(Intent arg0) { // TODO Auto-generated method stub return null; } }

现在,你想要得到的经度和纬度使用这种code:

Now where you want to get latitude and longitude use this code:

GPSTracker mTracker =新GPSTracker(MyActivity.this);
双纬度= mTracker.getLatitude();
双LNG = mTracker.getLongitude();

GPSTracker mTracker = new GPSTracker(MyActivity.this); double lat = mTracker.getLatitude(); double lng = mTracker.getLongitude();

这篇关于我怎么能getCurrent位置,也显示城市名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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