【问题标题】:Display location based on a place name rather than using location's longitude and latitude根据地名而不是使用位置的经度和纬度显示位置
【发布时间】:2013-12-18 04:29:48
【问题描述】:

我试图在我的 android 应用程序的地图中显示一个特定的地方,我能够使用这个地方的经度和纬度在地图上显示它。然而,信息气泡并未指向该地点,而是根据经度和纬度指向该位置。

我想在给定位置显示特定位置。

这是我的代码

double latitude = some value;
double longitude = some value;
String label = "Some text";
String loc = "geo:" + latitude + "," + longitude;
String query = latitude + "," + longitude + "(" + label + ")";
String encodedQuery = Uri.encode(query);
String uriLoc = loc + "?q=" + encodedQuery + "&z=16";
Log.d(uriLoc, "no log");
Uri uri = Uri.parse(uriLoc);
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, uri);
startActivity(intent);

【问题讨论】:

    标签: android google-maps


    【解决方案1】:

    你应该可以使用 Geocoder 来做到这一点:http://developer.android.com/reference/android/location/Geocoder.html

    【讨论】:

      【解决方案2】:

      作为suggested by user370305:

      Geocoder geocoder;
      List<Address> addresses;
      geocoder = new Geocoder(this, Locale.getDefault());
      addresses = geocoder.getFromLocation(latitude, longitude, 1);
      
      String address = addresses.get(0).getAddressLine(0);
      String city = addresses.get(0).getAddressLine(1);
      String country = addresses.get(0).getAddressLine(2);
      

      【讨论】:

        【解决方案3】:

        通过使用 getFromLocationName() 方法中的地名,我能够提取正确的经度和纬度值。

        代码

        Geocoder geocoder;
        List<Address> addresses = null;
        geocoder = new Geocoder(this, Locale.getDefault());
        try {
        //getFromLocationName returns array of address, it takes 2 parameter one place name and count of result
            addresses = geocoder.getFromLocationName("Place name", 1);
        } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }
        
        latitude = addresses.get(0).getLatitude();
        longitude = addresses.get(0).getLongitude();
        String label = "SomePlace";
        String in = "geo:" + latitude + "," + longitude;
        String query = latitude + "," + longitude + "(" + label + ")";
        String ey = Uri.encode(query);
        String uriLoc = in + "?q=" + ey+ "&z=16";
        Log.d(uriLoc, "no log");
        Uri uri = Uri.parse(uriLoc);
        Intent intent = new Intent(android.content.Intent.ACTION_VIEW, uri);
        startActivity(intent);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-10-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-03-30
          • 2018-02-15
          相关资源
          最近更新 更多