How to get coordinates of an address in android

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
public class MapActivity extends android.support.v4.app.FragmentActivity{
 
    private static final int MAP_ZOOM = 15;
    private String mAddress;
    private GoogleMap mMap;
    private double mLongitude;
    private double mlatitude;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map_hotel_car);
 
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            mAddress = extras.getString("Address");
        }
        mMap = ((SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map)).getMap();
 
        Log.v("ddd", mAddress);
 
        Geocoder geocoder = new Geocoder(this, Locale.getDefault());
                //To initialice list of address
        List<Address> addresses = null;
        try {
                        //To put the address in list
            addresses = geocoder.getFromLocationName(mAddress, 1);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
                //Get Latitude and longitude
        Address address = addresses.get(0);
        mLongitude = address.getLongitude();
        mlatitude = address.getLatitude();
        showMarker(mlatitude, mLongitude);
        centerMapOnMyLocation();
 
    }
 
    //show the poi in map
    private void showMarker(double lat, double lng)
    {
        mMap.addMarker(new MarkerOptions()
            .position(new LatLng(mlatitude, mLongitude))
            .title("Pais: España"));
    }
    //Center the point in map
    private void centerMapOnMyLocation() {
            mMap.moveCamera(CameraUpdateFactory.newLatLng(getMyLatLng()));
            mMap.animateCamera(CameraUpdateFactory.zoomTo(MAP_ZOOM));
    }
    private LatLng getMyLatLng() {
        return new LatLng(mlatitude, mLongitude);
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *