android - 将纬度和经度值转换为 google map api v2 中的地址

标签 android google-maps google-maps-api-2 reverse-geocoding

在我的应用程序中我得到了纬度和经度值,我想把它转换成相应的地址。但是当我这样做时我得到了错误

java.io.IOException: Service not Available at android.location.Geocoder.getFromLocation(Geocoder.java:136)

这是我的代码

import java.io.IOException;
import java.util.List;
import java.util.Locale;

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.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.maps.GeoPoint;

 public class MapActivity extends FragmentActivity {
private GoogleMap map;
//Location location;

    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
         LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

        LocationListener mlocListener = new GpsMapLocationActivity();
        mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);


        if (map == null) {
             map = ((SupportMapFragment)  getSupportFragmentManager().findFragmentById(R.id.map))
                       .getMap();

          map.setMyLocationEnabled(true);


        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.map, menu);
        return true;
    }



        private class GpsMapLocationActivity implements LocationListener{

            @Override
            public void onLocationChanged(Location location) {
                // TODO Auto-generated method stub
                if (location != null) {
                      /*final GeoPoint point = new GeoPoint(
                          (int) (location.getLatitude() * 1E6), 
                          (int) (location.getLongitude() * 1E6));*/

                    /*   String address = ConvertPointToLocation(point);address.toString();
                      Log.i("ADRESSS", ""+point);*/
                    double latitude=location.getLatitude();

                    double longitude=location.getLongitude();

                    LatLng loca=new LatLng(latitude,longitude);

                     String address = ConvertPointToLocation(loca);
                     address.toString();

                       Toast.makeText(getApplicationContext(),"" +loca,
                                  Toast.LENGTH_LONG).show();

                       Log.i("Adress",""+address);

                       CameraPosition  cmp= new CameraPosition.Builder().target(loca).zoom(14).bearing(90).tilt(30).build();
                       map.animateCamera(CameraUpdateFactory.newCameraPosition(cmp));

                     MarkerOptions marker = new MarkerOptions()
                    .position(loca)
                    .title("MyMap")
                    .snippet("My Map View")
                    .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_gps));
                     map.addMarker(marker);

}
}
    private String ConvertPointToLocation(LatLng loca) {
                // TODO Auto-generated method stub
                String address = "";
                  Geocoder geoCoder=new  Geocoder(getBaseContext(), Locale.getDefault());

                  try {
                      List<Address> addresses = geoCoder.getFromLocation(
                        loca.latitude ,
                        loca.longitude, 1);

                      if (addresses.size() > 0) {
                        for (int index = 0; 
                    index < addresses.get(0).getMaxAddressLineIndex(); index++)
                          address += addresses.get(0).getAddressLine(index) +  " ";
                      }
                    }
                    catch (IOException e) {        
                      e.printStackTrace();
                    }   

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

            }

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

            }

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

            }

        }



}

提前致谢..

最佳答案

SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    myMap = fm.getMap();
    // myMap.setMyLocationEnabled(true);
    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    String provider = locationManager.getBestProvider(criteria, true);
    final Location location = locationManager
            .getLastKnownLocation(provider);
    latitude = location.getLatitude();
    longitude = location.getLongitude();
    latLng = new LatLng(latitude, longitude);

    Geocoder geocoder = new Geocoder(MainActivity.this,
            Locale.getDefault());
    try {
        List<Address> addresses = geocoder.getFromLocation(latitude,
                longitude, 1);
        add = addresses.get(0).getAddressLine(0);
    }

    catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    currentloc = myMap.addMarker(new MarkerOptions()
            .position(latLng)
            .title(add)
            .snippet("" + latitude + "," + "" + longitude)
            .icon(BitmapDescriptorFactory
                    .fromResource(R.drawable.locdot)));

    myMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    myMap.animateCamera(CameraUpdateFactory.zoomTo(11), 2000, null);

    loc = (ImageButton) findViewById(R.id.loc);
    loc.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if (location != null) {
                onLocationChanged(location);
            }
        }
    });

    locationManager.requestLocationUpdates(provider, 20000, 0, this);

关于android - 将纬度和经度值转换为 google map api v2 中的地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19950311/

相关文章:

java - Android 中使用 VPN 的数据包嗅探器/过滤器

google-maps - Angular2 谷歌地图样式标记

java - 运行 google map api v2 时出错

android - surfaceView.getHolder 不返回 SurfaceHolder

java - 文件不追加 android

android - 如何在开发人员控制台中启用 "Google Maps Android API v2"

android - Android Maps V2 上的模糊自定义图 block

html - 静态谷歌地图在 iOS7.1 上不显示

android - 使用 Httpsurlconnection 连接到服务器 (SSL)

javascript - 如何更改此 javascript 中的标记图像