Does geocoder.getFromLocationName not work on emulators?

随声附和 提交于 2019-12-25 05:10:48

问题


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

    }

}

回答1:


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.




回答2:


What API version are you testing on? There appears to be a bug in earlier Android versions Geocoder works in 1.6 but not in 2.2 emulator

Also make sure your app has INTERNET permission (I assume it does as your map shows map tiles)

I always use real devices to test location functionality as I find it much easier, and closer to real-world scenarios.

Do note that the geocoder functionality is only available on official Android devices, i.e. those with Market/Gmail/Maps etc. So a low-cost Android device without the Android Market will fail to provide geocode results.



来源:https://stackoverflow.com/questions/9397836/does-geocoder-getfromlocationname-not-work-on-emulators

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!