7

Currently I am developing an android applicaiton. From my collection data, I have a list of location ( not address)- only name of location ( such as name of restaurant, resort, beach...), I want to find address of each location on Google Map API as the below image: enter image description here

How I can do it ?

I appreciate your help in this case. Thanks.

I'm also looking for a way to do it but I only find the solution to get Latitude or Longitude from specific address or in contrast.

I implemented as the below code but not result can be returned :(

    Geocoder coder = new Geocoder(this);
    List<Address> address;
    try {
    String locationName = "Nhà hàng Blanchy Street, VietNam";
    Geocoder gc = new Geocoder(this);
    List<Address> addressList = coder.getFromLocationName(locationName, 5);
    Address location = addressList.get(0);

    double latitude = location.getLatitude();
    double longitude = location.getLongitude();

        LatLng sydney = new LatLng(latitude , longitude );
        map.setMyLocationEnabled(true);
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));
user2659694
  • 1,160
  • 1
  • 12
  • 23
  • http://stackoverflow.com/questions/3490622/get-latitude-and-longitude-based-on-location-name-with-google-autocomplete-api – milez Jul 17 '15 at 07:07
  • I'm embedding Google Map API into my android application, not web. So I am not sure your link maybe useful for me in this case. Do you have any suggestions ? – user2659694 Jul 17 '15 at 07:10

2 Answers2

5

There is a Geocoder class, which you can use to find the address by location name String. The method is

Geocoder gc = new Geocoder(context);
List<Address> addresses = gc.getFromLocationName(String locationName, int maxResults);

This will give you a list of Address objects. Address has methods getLongitude() and getLatitude(), among others.

And since your map is embedded, the GoogleMap class has a method moveCamera(...) to go the location, if any is found with your location name.

For example:

String locationName = "Nha Hang restaurant";
Geocoder gc = new Geocoder(context);
List<Address> addressList = gc.getFromLocationName(locationName, 5);

Then either use the Address or get the longitude and latitude to create CameraUpdate with CameraUpdateFactory. With this try the moveCamera(...) if you wish.

milez
  • 2,201
  • 12
  • 31
  • However, I only have name of location( such as name of restaurant, beach, store...not address). So I can have specific address of this location, can't I ? – user2659694 Jul 17 '15 at 07:21
  • getFromLocationName(String locationName, int maxResults) should do just that. If the name of location is not in google database, then nothing is found. – milez Jul 17 '15 at 07:23
  • I already implemented as your suggestions but it seems to be I only get latitude, and longtitude. In this case I only have name of location as the picture which I posted above. address = coder.getFromLocationName( "York Street Kitchen,24 Erie St.Stratford, ON N5A 2M4,Canada", 5); Address location = address.get(0); double latitude = location.getLatitude(); double longitude = location.getLongitude(); – user2659694 Jul 17 '15 at 07:28
  • How come you used an address with the getFromLocationName method, you said you only know the name, not the address? There is a different method for finding coordinates from address. – milez Jul 17 '15 at 07:29
  • I added an example of finding the address by location name. Remember you can ask Address for getLongitude() and getLatitude() – milez Jul 17 '15 at 07:35
  • Thanks, I just tried as your example. However, the function map.moveCamera only implement with CameraUpdated not address as you posted :( – user2659694 Jul 17 '15 at 07:48
  • Well you should be able to create CameraUpdates with the address or the coordinates you can get from it. Google's example uses CameraUpdateFactory. – milez Jul 17 '15 at 07:54
  • I implemented as above code but not result can be returned. The size of Addresslist is 0. Do you know Goolge Map Autocomplete API. It will be very useful if we can have the first suggestion :( – user2659694 Jul 17 '15 at 08:05
  • Sorry but I have no expertise with autocomplete api :( – milez Jul 17 '15 at 08:07
  • It similar with the question on the link http://stackoverflow.com/questions/3490622/get-latitude-and-longitude-based-on-location-name-with-google-autocomplete-api but we are trying to implement on android application :( – user2659694 Jul 17 '15 at 08:08
0

If you don't have access to the latitude and longitude then, you have to use Google Places api for this. Using this you can get the list addresses with that name.

http://examples.javacodegeeks.com/android/android-google-places-autocomplete-api-example/

If you already have the latitude and longitude of the location, then you can use reverse geocoding for finding the address of location.

Geocoder gc = new Geocoder(context);

if(gc.isPresent()){
  List<Address> list = gc.getFromLocation(37.42279, -122.08506,1);

  Address address = list.get(0);

  StringBuffer str = new StringBuffer();
  str.append("Name: " + address.getLocality() + "\n");
  str.append("Sub-Admin Ares: " + address.getSubAdminArea() + "\n");
  str.append("Admin Area: " + address.getAdminArea() + "\n");
  str.append("Country: " + address.getCountryName() + "\n");
  str.append("Country Code: " + address.getCountryCode() + "\n");

  String strAddress = str.toString();
}

https://androidcookbook.com/Recipe.seam?recipeId=1454

Jossy Paul
  • 1,267
  • 14
  • 26
  • It is similar with what I tried above. but the list size is 0 for any location name :( – user2659694 Jul 17 '15 at 08:07
  • You just have to remove this sb.append("&components=country:gr"); from the code. This is used for filtering countries in Google places api. – Jossy Paul Jul 17 '15 at 08:11
  • I don't have any information except location name. So I am reading your first link which you posted.Hope that it will be useful for me.:) – user2659694 Jul 17 '15 at 08:14