【问题标题】:How to get coordinates of an address in android如何在android中获取地址的坐标
【发布时间】:2012-03-30 16:44:41
【问题描述】:

如何获取用户在 android 中输入的位置/地址的 gps 坐标?

【问题讨论】:

  • 我找不到准确的答案所以.. @herschel

标签: android google-maps google-maps-api-3 gps


【解决方案1】:
Geocoder geocoder = new Geocoder(<your context>);  
List<Address> addresses;
addresses = geocoder.getFromLocationName(<String address>, 1);
if(addresses.size() > 0) {
    double latitude= addresses.get(0).getLatitude();
    double longitude= addresses.get(0).getLongitude();
}

【讨论】:

  • @natali 我想说一件事......hmmmmm 你的头像非常非常非常......可爱:)..
【解决方案2】:

您可以使用Android的Geocoder进行反向地理编码:

Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocationName(myLocation, 1);
Address address = addresses.get(0);
double longitude = address.getLongitude();
double latitude = address.getLatitude();

AndroidManifest.xml 中还包括以下内容:

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

另请注意,您需要使用包含 Geocoder 实现的 API。例如,包含此内容的 API 是 Android Google API。您可以使用 Geocoder.isPresent() 检查目标 API 的实现是否存在。

查看Geocoderdocumentation了解更多信息。

【讨论】:

  • 这是为我做的 :-)
【解决方案3】:
List<Address> addresses;
addresses = geocoder.getFromLocationName(<String address>, 1);
if(addresses.size() > 0){
double latitude= addresses.get(0).getLatitude();
double longitude= addresses.get(0).getLongitude();
}

清单权限:-

android.permission.INTERNET
android.permission.ACCESS_COARSE_LOCATION
android.permission.ACCESS_FINE_LOCATION
android.permission.ACCESS_MOCK_LOCATION

【讨论】:

    【解决方案4】:

    使用清单权限,如

    android.permission.INTERNET 
    android.permission.ACCESS_COARSE_LOCATION     
    android.permission.ACCESS_FINE_LOCATION 
    android.permission.ACCESS_MOCK_LOCATION
    

    然后继续这个

    Geocoder geocoder = new Geocoder(this, Locale.getDefault());
    List<Address> addresses = geocoder.getFromLocationName(myLocation, 1);
    Address address = addresses.get(0);
    if(addresses.size() > 0) {
        double latitude = addresses.get(0).getLatitude();
        double longitude = addresses.get(0).getLongitude();
    }
    

    【讨论】:

    • 我们真的需要互联网连接来解码地理点吗??
    • @SANTHOSH 是的,当然,没有本地地址数据库。
    【解决方案5】:

    这是一个完整的示例

    首先定义一个缩放

        final CameraUpdate zoom = CameraUpdateFactory.zoomTo(5);
    

    清除所有地图标记

        mMap.clear();
        //Declare a new marker
        final MarkerOptions mp = new MarkerOptions();
        //declare a EditText to get the user informed address
        EditText etEndereco = findViewById(R.id.map_prof_etEnd);
    
        Geocoder geocoder = new Geocoder(map_prof.this);
        List<Address> addresses = null;
        try {
            addresses = geocoder.getFromLocationName(etEndereco.getText().toString(), 1);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (addresses.size() > 0) {
            double latitude = addresses.get(0).getLatitude();
            double longitude = addresses.get(0).getLongitude();
    
            mp.position(new LatLng(latitude, longitude));
    
            Log.e("teste map2", "inserted latitude " + Latitude + ", inserted Longitude " + Longitude);
    
            mp.title("Selected location");
            CameraUpdate center = CameraUpdateFactory.newLatLng(new LatLng(latitude, longitude));
            mMap.clear();
            mMap.addMarker(mp);
            mMap.moveCamera(center);
            mMap.animateCamera(zoom);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多