在android中找不到地址

在android中找不到地址,android,Android,我在这里做过编码 纬度和经度显示值,例如:纬度==12.9165282,经度==80.1522998和 gps类返回:“服务找不到地址:请开发者注意,如果谷歌自己找不到地址,你对此无能为力。” 通过联机将Lat和Long转换为地址 它返回的地址是:印度泰米尔纳德邦钦奈马达巴卡姆的埃里卡莱600073 在mycode中,它不会从google返回地址和地址 公共类MainActivity扩展了ActionBarActivity{ @Override protected void onCreate(B

我在这里做过编码

纬度和经度显示值,例如:纬度==12.9165282,经度==80.1522998和

gps类返回:“服务找不到地址:请开发者注意,如果谷歌自己找不到地址,你对此无能为力。”

通过联机将Lat和Long转换为地址 它返回的地址是:印度泰米尔纳德邦钦奈马达巴卡姆的埃里卡莱600073

在mycode中,它不会从google返回地址和地址

公共类MainActivity扩展了ActionBarActivity{

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

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }
}

@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;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);

        final TextView tvLocation = (TextView)rootView.findViewById(R.id.tvLocation);
        final TextView tvAddress = (TextView)rootView.findViewById(R.id.tvAddress);

        Button btnGetLocation = (Button)rootView.findViewById(R.id.btnGetLocation);

        btnGetLocation.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                String address = "";
                GPSService mGPSService = new GPSService(getActivity());
                mGPSService.getLocation();

                if (mGPSService.isLocationAvailable == false) {

                    // Here you can ask the user to try again, using return; for that
                    Toast.makeText(getActivity(), "Your location is not available, please try again.", Toast.LENGTH_SHORT).show();
                    return;

                    // Or you can continue without getting the location, remove the return; above and uncomment the line given below
                    // address = "Location not available";
                } else {

                    // Getting location co-ordinates
                    double latitude = mGPSService.getLatitude();
                    double longitude = mGPSService.getLongitude();
                    Toast.makeText(getActivity(), "Latitude:" + latitude + " | Longitude: " + longitude, Toast.LENGTH_LONG).show();

                    address = mGPSService.getLocationAddress();

                    tvLocation.setText("Latitude: " + latitude + " \nLongitude: " + longitude);
                    tvAddress.setText("Address: " + address);
                }

                Toast.makeText(getActivity(), "Your address is: " + address, Toast.LENGTH_SHORT).show();

                // make sure you close the gps after using it. Save user's battery power
                mGPSService.closeGPS();


            }
        });







        return rootView;
    }
}
这节课是为gpsserice准备的

        public class GPSService extends Service implements LocationListener {

// saving the context for later use
private final Context mContext;

// if GPS is enabled
boolean isGPSEnabled = false;
// if Network is enabled
boolean isNetworkEnabled = false;
// if Location co-ordinates are available using GPS or Network
public boolean isLocationAvailable = false;

// Location and co-ordinates coordinates
Location mLocation;
double mLatitude;
double mLongitude;

// Minimum time fluctuation for next update (in milliseconds)
private static final long TIME = 30000;
// Minimum distance fluctuation for next update (in meters)
private static final long DISTANCE = 20;

// Declaring a Location Manager
protected LocationManager mLocationManager;

public GPSService(Context context) {
    this.mContext = context;
    mLocationManager = (LocationManager) mContext
            .getSystemService(LOCATION_SERVICE);

}

/**
 * Returs the Location
 * 
 * @return Location or null if no location is found
 */
public Location getLocation() {
    try {

        // Getting GPS status
        isGPSEnabled = mLocationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);

        // If GPS enabled, get latitude/longitude using GPS Services
        if (isGPSEnabled) {
            mLocationManager.requestLocationUpdates(
                    LocationManager.GPS_PROVIDER, TIME, DISTANCE, this);
            if (mLocationManager != null) {
                mLocation = mLocationManager
                        .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                if (mLocation != null) {
                    mLatitude = mLocation.getLatitude();
                    mLongitude = mLocation.getLongitude();
                    isLocationAvailable = true; // setting a flag that
                                                // location is available
                    return mLocation;
                }
            }
        }

        // If we are reaching this part, it means GPS was not able to fetch
        // any location
        // Getting network status
        isNetworkEnabled = mLocationManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (isNetworkEnabled) {
            mLocationManager.requestLocationUpdates(
                    LocationManager.NETWORK_PROVIDER, TIME, DISTANCE, this);
            if (mLocationManager != null) {
                mLocation = mLocationManager
                        .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                if (mLocation != null) {
                    mLatitude = mLocation.getLatitude();
                    mLongitude = mLocation.getLongitude();
                    isLocationAvailable = true; // setting a flag that
                                                // location is available
                    return mLocation;
                }
            }
        }
        // If reaching here means, we were not able to get location neither
        // from GPS not Network,
        if (!isGPSEnabled) {
            // so asking user to open GPS
            askUserToOpenGPS();
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    // if reaching here means, location was not available, so setting the
    // flag as false
    isLocationAvailable = false;
    return null;
}

/**
 * Gives you complete address of the location
 * 
 * @return complete address in String
 */
public String getLocationAddress() {

    if (isLocationAvailable) {

        Geocoder geocoder = new Geocoder(mContext, Locale.getDefault());
        // Get the current location from the input parameter list
        // Create a list to contain the result address
        List<Address> addresses = null;
        try {
            /*
             * Return 1 address.
             */
            addresses = geocoder.getFromLocation(mLatitude, mLongitude, 1);
        } catch (IOException e1) {
            e1.printStackTrace();
            return ("IO Exception trying to get address:" + e1);
        } catch (IllegalArgumentException e2) {
            // Error message to post in the log
            String errorString = "Illegal arguments "
                    + Double.toString(mLatitude) + " , "
                    + Double.toString(mLongitude)
                    + " passed to address service";
            e2.printStackTrace();
            return errorString;
        }
        // If the reverse geocode returned an address
        if (addresses != null && addresses.size() > 0) {
            // Get the first address
            Address address = addresses.get(0);
            /*
             * Format the first line of address (if available), city, and
             * country name.
             */
            String addressText = String.format(
                    "%s, %s, %s",
                    // If there's a street address, add it
                    address.getMaxAddressLineIndex() > 0 ? address
                            .getAddressLine(0) : "",
                    // Locality is usually a city
                    address.getLocality(),
                    // The country of the address
                    address.getCountryName());
            // Return the text
            return addressText;
        } else {
            return "No address found by the service: Note to the developers, If no address is found by google itself, there is nothing you can do about it.";
        }
    } else {
        return "Location Not available";
    }

}



/**
 * get latitude
 * 
 * @return latitude in double
 */
public double getLatitude() {
    if (mLocation != null) {
        mLatitude = mLocation.getLatitude();
    }
    return mLatitude;
}

/**
 * get longitude
 * 
 * @return longitude in double
 */
public double getLongitude() {
    if (mLocation != null) {
        mLongitude = mLocation.getLongitude();
    }
    return mLongitude;
}

/**
 * close GPS to save battery
 */
public void closeGPS() {
    if (mLocationManager != null) {
        mLocationManager.removeUpdates(GPSService.this);
    }
}

/**
 * show settings to open GPS
 */
public void askUserToOpenGPS() {
    AlertDialog.Builder mAlertDialog = new AlertDialog.Builder(mContext);

    // Setting Dialog Title
    mAlertDialog.setTitle("Location not available, Open GPS?")
    .setMessage("Activate GPS to use use location services?")
    .setPositiveButton("Open Settings", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            mContext.startActivity(intent);
            }
        })
        .setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
                }
            }).show();
}

/** 
 * Updating the location when location changes
 */
@Override
public void onLocationChanged(Location location) {
    mLatitude = location.getLatitude();
    mLongitude = location.getLongitude();
}

@Override
public void onProviderDisabled(String provider) {
}

@Override
public void onProviderEnabled(String provider) {
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}

@Override
public IBinder onBind(Intent arg0) {
    return null;
}
公共类GPSService扩展服务实现LocationListener{
//保存上下文以供以后使用
私有最终上下文mContext;
//如果启用了GPS
布尔值isGPSEnabled=false;
//如果网络已启用
布尔值isNetworkEnabled=false;
//如果位置坐标可用GPS或网络
公共布尔值isLocationAvailable=false;
//位置和坐标
位置;
双重疲劳;
双倍长度;
//下一次更新的最短时间波动(毫秒)
私人静态最终长时间=30000;
//下次更新的最小距离波动(以米为单位)
私人静态最终长途=20;
//声明位置管理器
受保护的位置管理器mLocationManager;
公共GPSService(上下文){
this.mContext=上下文;
mLocationManager=(LocationManager)mContext
.getSystemService(位置服务);
}
/**
*重新定位
* 
*@返回位置,如果找不到位置,则返回null
*/
公共位置getLocation(){
试一试{
//获取GPS状态
isGPSEnabled=mLocationManager
.isprovidenabled(LocationManager.GPS\U提供商);
//如果启用了GPS,请使用GPS服务获取纬度/经度
如果(isGPSEnabled){
mLocationManager.RequestLocationUpdate(
LocationManager.GPS_提供程序、时间、距离、此);
if(mLocationManager!=null){
mLocation=mLocationManager
.getLastKnownLocation(LocationManager.GPS\U提供商);
如果(mLocation!=null){
mLatitude=mLocation.getLatitude();
mlongalite=mLocation.getLongitude();
isLocationAvailable=true;//设置一个
//位置是可用的
返回位置;
}
}
}
//如果我们到达这一部分,这意味着GPS无法获取数据
//任何地点
//获取网络状态
isNetworkEnabled=mlLocationManager
.isProviderEnabled(LocationManager.NETWORK_提供商);
if(可联网){
mLocationManager.RequestLocationUpdate(
LocationManager.NETWORK_提供程序、时间、距离、此);
if(mLocationManager!=null){
mLocation=mLocationManager
.getLastKnownLocation(LocationManager.网络提供商);
如果(mLocation!=null){
mLatitude=mLocation.getLatitude();
mlongalite=mLocation.getLongitude();
isLocationAvailable=true;//设置一个
//位置是可用的
返回位置;
}
}
}
//如果到达这里意味着,我们也无法获得位置
//从全球定位系统而不是网络,
如果(!isGPSEnabled){
//所以要求用户打开GPS
askUserToOpenGPS();
}
}捕获(例外e){
e、 printStackTrace();
}
//如果到达此处意味着位置不可用,则设置
//标志为假
isLocationAvailable=false;
返回null;
}
/**
*提供该位置的完整地址
* 
*@返回字符串形式的完整地址
*/
公共字符串getLocationAddress(){
如果(isLocationAvailable){
Geocoder Geocoder=新的地理编码器(mContext,Locale.getDefault());
//从输入参数列表中获取当前位置
//创建包含结果地址的列表
列表地址=空;
试一试{
/*
*返回1个地址。
*/
地址=地理编码器.getFromLocation(mLatitude,mLongitate,1);
}捕获(IOE1异常){
e1.printStackTrace();
返回(“试图获取地址的IO异常:”+e1);
}捕获(IllegalArgumentException e2){
//要在日志中发布的错误消息
String errorString=“非法参数”
+双.toString(mLatitude)+“,”
+双.toString(长度)
+“传递到地址服务”;
e2.printStackTrace();
返回错误字符串;
}
//如果反向地理代码返回一个地址
if(addresses!=null&&addresses.size()>0){
//获取第一个地址
地址=地址。获取(0);
/*
*格式化第一行地址(如果可用)、城市和
*国名。
*/
字符串地址text=String.format(
“%s,%s,%s”,
//如果有街道地址,请添加它
address.getMaxAddressLineIndex()>0?地址
.getAddressLine(0):“”,
//地点通常是一个城市
address.getLocation(),
//地址所在国
address.getCountryName());
//返回文本
返回地址文本;
}否则{
return“找不到地址”
public void getUserAddress(LatLng latLng) {
    Geocoder geocoder;
    List<Address> addresses = null;
    String errorMessage = getString(R.string.unknown_address);
    geocoder = new Geocoder(mContext, Locale.getDefault());
    try {
        addresses = geocoder.getFromLocation(latLng.latitude, latLng.longitude, 1);
    } catch (IOException e) {
        e.printStackTrace();

    }
    if (addresses != null) {
        if (addresses.size() > 0) {
            String address = addresses.get(0).getAddressLine(0);
            String city = addresses.get(0).getLocality();
            return address + " , " + city;
        } else
            return errorMessage;

    } else
        return errorMessage;

}