Android 如何从地址中找到经纬度?

Android 如何从地址中找到经纬度?,android,google-maps,google-geocoder,Android,Google Maps,Google Geocoder,我想在谷歌地图中显示地址的位置 如何使用Google Maps API获取地址的纬度和经度?公共地理点getLocationFromAddress(字符串){ public GeoPoint getLocationFromAddress(String strAddress){ Geocoder coder = new Geocoder(this); List<Address> address; GeoPoint p1 = null; try { address = cod

我想在谷歌地图中显示地址的位置

如何使用Google Maps API获取地址的纬度和经度?

公共地理点getLocationFromAddress(字符串){
public GeoPoint getLocationFromAddress(String strAddress){

Geocoder coder = new Geocoder(this);
List<Address> address;
GeoPoint p1 = null;

try {
    address = coder.getFromLocationName(strAddress,5);
    if (address==null) {
       return null;
    }
    Address location=address.get(0);
    location.getLatitude();
    location.getLongitude();

    p1 = new GeoPoint((double) (location.getLatitude() * 1E6),
                      (double) (location.getLongitude() * 1E6));

    return p1;
    }
}
地理编码器=新地理编码器(本); 名单地址; 地球点p1=零; 试一试{ 地址=编码者。getFromLocationName(StradAddress,5); 如果(地址==null){ 返回null; } 地址位置=地址。获取(0); location.getLatitude(); location.getLongitude(); p1=新的地质点((双)(location.getLatitude()*1E6), (double)(location.getLongitude()*1E6)); 返回p1; } }

strAddress
是包含地址的字符串。
address
变量保存转换后的地址。

这是如何找到我们在地图上单击的位置的纬度和经度

public boolean onTouchEvent(MotionEvent event, MapView mapView) 
{   
    //---when user lifts his finger---
    if (event.getAction() == 1) 
    {                
        GeoPoint p = mapView.getProjection().fromPixels(
            (int) event.getX(),
            (int) event.getY());

        Toast.makeText(getBaseContext(), 
             p.getLatitudeE6() / 1E6 + "," + 
             p.getLongitudeE6() /1E6 , 
             Toast.LENGTH_SHORT).show();
    }                            
    return false;
} 
它工作得很好


要获取位置的地址,我们可以使用geocoder类。

如果您想在谷歌地图中放置您的地址,请使用以下简单方法

Intent searchAddress = new  Intent(Intent.ACTION_VIEW,Uri.parse("geo:0,0?q="+address));
startActivity(searchAddress);
public static JSONObject getLocationInfo(String address) {
        StringBuilder stringBuilder = new StringBuilder();
        try {

        address = address.replaceAll(" ","%20");    

        HttpPost httppost = new HttpPost("http://maps.google.com/maps/api/geocode/json?address=" + address + "&sensor=false");
        HttpClient client = new DefaultHttpClient();
        HttpResponse response;
        stringBuilder = new StringBuilder();


            response = client.execute(httppost);
            HttpEntity entity = response.getEntity();
            InputStream stream = entity.getContent();
            int b;
            while ((b = stream.read()) != -1) {
                stringBuilder.append((char) b);
            }
        } catch (ClientProtocolException e) {
        } catch (IOException e) {
        }

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject = new JSONObject(stringBuilder.toString());
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return jsonObject;
    }
public static boolean getLatLong(JSONObject jsonObject) {

        try {

            longitute = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
                .getJSONObject("geometry").getJSONObject("location")
                .getDouble("lng");

            latitude = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
                .getJSONObject("geometry").getJSONObject("location")
                .getDouble("lat");

        } catch (JSONException e) {
            return false;

        }

        return true;
    }

如果您需要从您的地址获取lat long,请使用下面的Google Place Api

Intent searchAddress = new  Intent(Intent.ACTION_VIEW,Uri.parse("geo:0,0?q="+address));
startActivity(searchAddress);
public static JSONObject getLocationInfo(String address) {
        StringBuilder stringBuilder = new StringBuilder();
        try {

        address = address.replaceAll(" ","%20");    

        HttpPost httppost = new HttpPost("http://maps.google.com/maps/api/geocode/json?address=" + address + "&sensor=false");
        HttpClient client = new DefaultHttpClient();
        HttpResponse response;
        stringBuilder = new StringBuilder();


            response = client.execute(httppost);
            HttpEntity entity = response.getEntity();
            InputStream stream = entity.getContent();
            int b;
            while ((b = stream.read()) != -1) {
                stringBuilder.append((char) b);
            }
        } catch (ClientProtocolException e) {
        } catch (IOException e) {
        }

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject = new JSONObject(stringBuilder.toString());
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return jsonObject;
    }
public static boolean getLatLong(JSONObject jsonObject) {

        try {

            longitute = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
                .getJSONObject("geometry").getJSONObject("location")
                .getDouble("lng");

            latitude = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
                .getJSONObject("geometry").getJSONObject("location")
                .getDouble("lat");

        } catch (JSONException e) {
            return false;

        }

        return true;
    }
创建一个方法,返回一个JSONObject,并响应HTTP调用,如下所示

Intent searchAddress = new  Intent(Intent.ACTION_VIEW,Uri.parse("geo:0,0?q="+address));
startActivity(searchAddress);
public static JSONObject getLocationInfo(String address) {
        StringBuilder stringBuilder = new StringBuilder();
        try {

        address = address.replaceAll(" ","%20");    

        HttpPost httppost = new HttpPost("http://maps.google.com/maps/api/geocode/json?address=" + address + "&sensor=false");
        HttpClient client = new DefaultHttpClient();
        HttpResponse response;
        stringBuilder = new StringBuilder();


            response = client.execute(httppost);
            HttpEntity entity = response.getEntity();
            InputStream stream = entity.getContent();
            int b;
            while ((b = stream.read()) != -1) {
                stringBuilder.append((char) b);
            }
        } catch (ClientProtocolException e) {
        } catch (IOException e) {
        }

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject = new JSONObject(stringBuilder.toString());
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return jsonObject;
    }
public static boolean getLatLong(JSONObject jsonObject) {

        try {

            longitute = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
                .getJSONObject("geometry").getJSONObject("location")
                .getDouble("lng");

            latitude = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
                .getJSONObject("geometry").getJSONObject("location")
                .getDouble("lat");

        } catch (JSONException e) {
            return false;

        }

        return true;
    }
现在将JSONObject传递给getLatLong()方法,如下所示

Intent searchAddress = new  Intent(Intent.ACTION_VIEW,Uri.parse("geo:0,0?q="+address));
startActivity(searchAddress);
public static JSONObject getLocationInfo(String address) {
        StringBuilder stringBuilder = new StringBuilder();
        try {

        address = address.replaceAll(" ","%20");    

        HttpPost httppost = new HttpPost("http://maps.google.com/maps/api/geocode/json?address=" + address + "&sensor=false");
        HttpClient client = new DefaultHttpClient();
        HttpResponse response;
        stringBuilder = new StringBuilder();


            response = client.execute(httppost);
            HttpEntity entity = response.getEntity();
            InputStream stream = entity.getContent();
            int b;
            while ((b = stream.read()) != -1) {
                stringBuilder.append((char) b);
            }
        } catch (ClientProtocolException e) {
        } catch (IOException e) {
        }

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject = new JSONObject(stringBuilder.toString());
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return jsonObject;
    }
public static boolean getLatLong(JSONObject jsonObject) {

        try {

            longitute = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
                .getJSONObject("geometry").getJSONObject("location")
                .getDouble("lng");

            latitude = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
                .getJSONObject("geometry").getJSONObject("location")
                .getDouble("lat");

        } catch (JSONException e) {
            return false;

        }

        return true;
    }
我希望这对你和其他人都有帮助。。!!
谢谢你

以下代码适用于google apiv2:

public void convertAddress() {
    if (address != null && !address.isEmpty()) {
        try {
            List<Address> addressList = geoCoder.getFromLocationName(address, 1);
            if (addressList != null && addressList.size() > 0) {
                double lat = addressList.get(0).getLatitude();
                double lng = addressList.get(0).getLongitude();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } // end catch
    } // end if
} // end convertAddress
公共地址(){
if(address!=null&&!address.isEmpty()){
试一试{
List addressList=geoCoder.getFromLocationName(地址,1);
if(addressList!=null&&addressList.size()>0){
double lat=addressList.get(0.getLatitude();
double lng=addressList.get(0.getLongitude();
}
}捕获(例外e){
e、 printStackTrace();
}//端盖
}//如果结束,则结束
}//结束地址

其中address是要转换为LatLng的字符串(123测试第三城市州zip)。

以上坎达问题的答案:

它抛出“java.io.IOException服务不可用”
我已经给了那些权限,包括库…我可以得到地图视图…它在geocoder抛出IOException

我只是在尝试之后添加了一个catch IOException,它解决了问题

    catch(IOException ioEx){
        return null;
    }

Ud_an的解决方案和更新的API

注意:该类是Google Play服务的一部分

强制要求

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

<uses-permission android:name="android.permission.INTERNET"/>

更新:如果您有目标SDK 23及更高版本,请确保您拥有位置的运行时权限

public LatLng getLocationFromAddress(Context context,String strAddress) {

    Geocoder coder = new Geocoder(context);
    List<Address> address;
    LatLng p1 = null;

    try {
        // May throw an IOException
        address = coder.getFromLocationName(strAddress, 5);
        if (address == null) {
            return null;
        }

        Address location = address.get(0);
        p1 = new LatLng(location.getLatitude(), location.getLongitude() );

    } catch (IOException ex) {

        ex.printStackTrace();
    }

    return p1;
}
public LatLng getLocationFromAddress(上下文,字符串){
地理编码器=新地理编码器(上下文);
名单地址;
LatLng p1=null;
试一试{
//可能会抛出一个异常
地址=编码者。getFromLocationName(StradAddress,5);
如果(地址==null){
返回null;
}
地址位置=地址。获取(0);
p1=新车床(location.getLatitude(),location.getLongitude());
}捕获(IOEX异常){
例如printStackTrace();
}
返回p1;
}
Geocoder coder=新的地理编码器(本);
列出地址;
试一试{
地址=编码器。getFromLocationName(地址,5);
如果(地址==null){
}
地址位置=地址。获取(0);
双纬度=location.getLatitude();
double lng=location.getLongitude();
对数i(“Lat”,“Lat+Lat”);
Log.i(“液化天然气”和“液化天然气”);
LatLng LatLng=新LatLng(lat,lng);
MarkerOptions MarkerOptions=新MarkerOptions();
标记选项位置(板条);
googleMap.addMarker(markerOptions);
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng,12));
}捕获(IOE异常){
e、 printStackTrace();
}
公共无效goToLocationFromAddress(字符串){
//使用活动上下文创建编码器-此
地理编码器=新地理编码器(本);
名单地址;
试一试{
//从绳子上取板条
地址=编码者。getFromLocationName(StradAddress,5);
//检查空值
if(地址!=null){
//让我们从所有可能性中选取第一种可能性。
试一试{
地址位置=地址。获取(0);
LatLng LatLng=新LatLng(location.getLatitude(),location.getLongitude());
//在该地图位置上设置动画并缩放
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(15));
}捕获(IndexOutOfBoundsException器){
Toast.makeText(此“位置不可用”,Toast.LENGTH_SHORT.show();
}
}
}捕获(IOE异常){
e、 printStackTrace();
}
}
公共LatLang getLatLangFromAddress(字符串){
Geocoder coder=新的Geocoder(这个,Locale.getDefault());
名单地址;
试一试{
地址=编码者。getFromLocationName(StradAddress,5);
如果(地址==null){
返回新的LatLang(-10000,-10000);
}
地址位置=地址。获取(0);
返回新的LatLang(location.getLatitude(),location.getLength());
}捕获(IOE异常){
返回新的LatLang(-10000,-10000);
}
}            
LatLang
在本例中是一个pojo类


不需要权限。

它抛出“java.io.IOException service not available”您需要正确的权限才能访问该服务你正在构建哪个android api版本的应用程序你需要有google api可用我已经用goog构建了