【问题标题】:Map Search Bar in Google Maps v2Google Maps v2 中的地图搜索栏
【发布时间】:2013-06-12 08:34:58
【问题描述】:

我想在Google Maps v2 的顶部添加一个“搜索栏”,如我从 Google play 中截取的这张小图片所示。

我该怎么做呢?谢谢

【问题讨论】:

  • 我已经有了地图,并使用 setLocationEnabled() 在最右侧添加了位置“按钮”
  • 也许这可以帮助你wptrafficanalyzer.in/blog/…
  • @Yume117,您应该写下您的评论作为答案。它也对我有用。感谢您的链接!
  • @An-droid 我使用的是 wptrafficanalyzer 而不是 public class MainActivity extends FragmentActivity ,我使用的是 public class MainActivity extends Activity。一切正常,除非我单击按钮搜索我的应用程序崩溃了。出了什么问题?

标签: android google-maps-api-2


【解决方案1】:

这将是一个迟到的答案,但对于这个非常普遍的需求应该有一个答案。

这里我们基本上需要做的是使用Google Geocoder在文本框中搜索用户提到的地址。

我们可以这样做,

Geocoder geo = new Geocoder(getBaseContext());
List<Address> gotAddresses = null;
try {
    gotAddresses = geocoder.getFromLocationName(locationName[0], 1);
} catch (IOException e) {
    e.printStackTrace();
}

此方法返回一个可用地址列表,在此代码中我们只接受一个地址,如果需要,我们可以通过更改上述方法中最后一个参数的值来获取多个地址。那么,

Address address = (Address) gotAddresses.get(0);

LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());

String properAddress = String.format("%s, %s",
address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",
address.getCountryName());

mMap.addMarker(new MarkerOptions()
            .position(new LatLng(address.getLatitude(), address.getLongitude())).draggable(true)
            .title(properAddress)
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.pin)));

【讨论】:

    猜你喜欢
    • 2013-07-21
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 2013-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多