难道地理coder.getFromLocationName不能在模拟器工作? [英] Does geocoder.getFromLocationName not work on emulators?

查看:181
本文介绍了难道地理coder.getFromLocationName不能在模拟器工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个应用程序,将用户输入,然后中心对用户输入的内容的地图。我已经正确安装了谷歌地图API。地图上显示出来。我也做了一定的仿真器是谷歌的API。我测试了我的code和地缘coder.getFromLocationName返回null。我已经看过了这个问题,并尝试了不同的解决方案,没有工作过。这里是我的code。

 公共类的GetURL扩展MapActivity {
地理codeR地理codeR;
MapView类MapView类= NULL;
/ **当第一次创建活动调用。 * /
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);       图形页面=(图形页面)findViewById(R.id.geoMap);
        mapView.setBuiltInZoomControls(真);
           INT纬度=(INT)(30.334954 * 1000000);
            INT LNG =(INT)( - 81.5625 * 1000000);
        GeoPoint的PT =新的GeoPoint(纬度,经度);
        mapView.getController()setZoom(10)。
        mapView.getController()setCenter(PT)。
        。mapView.getController()animateTo(PT);        地理codeR =新的地缘codeR(本);}公共无效first_location(查看为arg0){
    尝试{
          LOC的EditText =(EditText上)findViewById(R.id.location);
            。字符串LOCATIONNAME = loc.getText()的toString();            清单<地址> AddressList中=
                   地理coder.getFromLocationName(LOCATIONNAME,5);
            如果(AddressList中=空&放大器;!&放大器; addressList.size()大于0)
            {                INT纬度=(INT)(addressList.get(0).getLatitude()* 1000000);
                INT LNG =(INT)(addressList.get(0).getLongitude()* 1000000);                GeoPoint的PT =新的GeoPoint(纬度,经度);
                mapView.getController()setZoom(15)。
                mapView.getController()setCenter(PT)。
                。mapView.getController()animateTo(PT);
        }
    }赶上(IOException异常五){
        e.printStackTrace();    }}


解决方案

如果您的应用程序工作的真实设备上,那么你的code应该罚款。地理编码看作公司有认购或购买为他们的设备的服务。我想大多数人只使用谷歌,但他们可能会使用Bing或其他一些为好。

的文档:


  

的地理位置codeR类需要不包含在后端服务
  Android核心框架。地缘codeR查询方法将返回
  空列表如果在平台没有后端服务。使用
  是present()方法来确定是否一个地理codeR实施
  存在。


在我的经验,呼吁地理coder.is present()从模拟器将返回false,并从真正的手机将返回true,只要真正的手机是你听说过的公司生产的。

如果你想围绕一个工作/备份服务,可以查询通过HTTP谷歌地图API 就像你从网页上会。这里有一个在这个答案一个例子。

I am working on an app that takes the user input and then center the map on what the user entered. I have properly installed the google maps API. The map shows up. I also made sure the emulator is google API. I have tested my code and the geocoder.getFromLocationName returns null. I have looked up this questions and have tried different solutions and none have worked. Here is my code.

 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();

    }

}

If your app works on a real device, then your code should be fine. Think of geocoding as a service that companies have to subscribe or buy in to for their devices. I think most of them just use Google, but they could probably use Bing or some other as well.

From the docs:

The Geocoder class requires a backend service that is not included in the core android framework. The Geocoder query methods will return an empty list if there no backend service in the platform. Use the isPresent() method to determine whether a Geocoder implementation exists.

In my experience, calling geocoder.isPresent() from the emulator will return false, and from a real phone will return true, as long as the real phone is made by a company you've heard of.

If you want a work around/backup service, you can query the Google Maps API through HTTP just like you would from a web page. There's an example in this answer here.

这篇关于难道地理coder.getFromLocationName不能在模拟器工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆