Androider
Androider

Reputation: 61

Using Maps in Android

I have the address to certain location in terms of a string. However i want to convert this address to Lattitude and Longitude. So i used the code given below to convert into geopoint. But this geopoint is not getting added to the map. The map always shows the default map thats the entire world.

public GeoPoint getLocationFromAddress(String strAddress) {

        Geocoder coder = new Geocoder(this);
        List<Address> address;
        GeoPoint p1 = null;

        try {
            address = coder.getFromLocationName(strAddress, 5);
            if (address == null) {
                return null;
            }
            Address location = address.get(0);
            location.getLatitude();
            location.getLongitude();

            p1 = new GeoPoint((int) (location.getLatitude() * 1E6),
                    (int) (location.getLongitude() * 1E6));

            return p1;
        } catch (IOException e) {
            e.printStackTrace();
            return p1;
        }
    }

Im new with maps and am not sure what Im doing wrong. Any suggestions?

Upvotes: 1

Views: 60

Answers (1)

Francesco verheye
Francesco verheye

Reputation: 1574

Now that you know the latitude and longitude, you forgot to update the map to the specific location. You can do it like this:

int zoomNiveau = 15;    
map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), zoomNiveau));

With this code, the map will go to the location of the address. But you need to create a Marker and add it to the map if you want a Marker.

More info about the markers: https://developers.google.com/maps/documentation/android/marker

Upvotes: 2

Related Questions