0

我正在开发一个应用程序,该应用程序接受用户输入,然后将地图以用户输入的内容居中。我已经正确安装了谷歌地图 API。地图出现。我还确保模拟器是谷歌 API。我已经测试了我的代码,并且 geocoder.getFromLocationName 返回 null。我已经查找了这个问题并尝试了不同的解决方案,但都没有奏效。这是我的代码。

 public class GetURL extends MapActivity {
Geocoder geocoder;
MapView mapView = null;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

       mapView = (MapView)findViewById(R.id.geoMap);
        mapView.setBuiltInZoomControls(true);


           int lat = (int)(30.334954*1000000);
            int lng = (int)(-81.5625*1000000);
        GeoPoint pt = new GeoPoint(lat,lng);
        mapView.getController().setZoom(10);
        mapView.getController().setCenter(pt);
        mapView.getController().animateTo(pt);

        geocoder = new Geocoder(this);

}

public void first_location(View arg0) {
    try {
          EditText loc = (EditText)findViewById(R.id.location);
            String locationName = loc.getText().toString();

            List<Address> addressList = 
                   geocoder.getFromLocationName(locationName, 5);
            if(addressList!=null && addressList.size()>0)
            {

                int lat = (int)(addressList.get(0).getLatitude()*1000000);
                int lng = (int)(addressList.get(0).getLongitude()*1000000);

                GeoPoint pt = new GeoPoint(lat,lng);
                mapView.getController().setZoom(15);
                mapView.getController().setCenter(pt);
                mapView.getController().animateTo(pt);
        }
    } catch (IOException e) {
        e.printStackTrace();

    }

}
4

2 回答 2

4

如果您的应用程序可以在真实设备上运行,那么您的代码应该没问题。将地理编码视为公司必须为其设备订阅或购买的服务。我认为他们中的大多数人只使用 Google,但他们也可能使用 Bing 或其他一些。

文档

Geocoder 类需要一个不包含在核心 android 框架中的后端服务。如果平台中没有后端服务,Geocoder 查询方法将返回一个空列表。使用 isPresent() 方法确定是否存在 Geocoder 实现。

根据我的经验,geocoder.isPresent()从模拟器调用将返回 false,而从真实电话中调用将返回 true,只要真实电话是由您听说过的公司制造的。

如果您想要解决/备份服务,您可以像从网页中一样通过 HTTP查询Google Maps API 。这个答案中有一个例子here

于 2012-02-22T15:59:57.073 回答
0

您正在测试哪个 API 版本?在早期的 Android 版本中似乎有一个错误 Geocoder 在 1.6 中工作,但在 2.2 模拟器中没有

还要确保您的应用程序具有 INTERNET 权限(我假设它是因为您的地图显示地图图块)

我总是使用真实设备来测试定位功能,因为我发现它更容易,更接近真实世界的场景。

请注意,地理编码器功能仅适用于官方 Android 设备,即具有 Market/Gmail/Maps 等的设备。因此,没有 Android Market 的低成本 Android 设备将无法提供地理编码结果。

于 2012-02-22T15:39:29.507 回答