开发者

google maps API and geocoder.getFromLocationName

开发者 https://www.devze.com 2023-01-22 17:05 出处:网络
I want to search for GPS locations for with Google Maps. I have already registered with Google Maps API, and got the key. I can successfully pinpoint my current location on a map. The next part is to

I want to search for GPS locations for with Google Maps. I have already registered with Google Maps API, and got the key. I can successfully pinpoint my current location on a map. The next part is to search for items around the current GPS location.

Approach 1: I tried using Android's geocoder.getFromLocationName("UPS",5) but I am not getting anything.

Approach 2: hit Google https://maps.googleapis.cm/maps开发者_开发百科/apo/place/search but it needs a client id. To get a client id I have to create a premier account. Do I have to do all this?

Any suggestion how to use maps to search location for Android?


"GPS" is the place you're looking up? Not a city or street name? I think that's the problem here.

Maybe look at: How can I find the latitude and longitude from address?


You will want to use a better "address" other than UPS to find locations using Geocoder. Do you realize how many UPS locations will be found. getFromLocationName() is best used with detailed location info and a locale directive so it limits its searching to the Locale you specify. Also, you will frequently get IOExceptions in the emulator. Lastly using SDK 2.3 and above will not work with Geocoder.

Try something like this:

Geocoder geocoder = new Geocoder(this, Locale.US);

        String staddress = "Georgia Tech, Atlanta, GA";

        try{
         List<Address> loc = geocoder.getFromLocationName(staddress, 5);
        }
        catch(IOException e) {
         Log.e("IOException", e.getMessage()); 
         Toast.makeText(this, "IOException:  " + e.getMessage(),20).show();

Notice how the locale is set to US when I new up my Geocoder. Also notice the try catch around the call to geocoder. That will catch any IO Exceptions and allow you ot handle them as I did using a Toast message and some other flow control statements that are out of sight

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号