How to get coordinates of an address in android

后端 未结 5 1998
[愿得一人]
[愿得一人] 2021-01-30 04:29

How do i get gps co-ordinates of the location/address entered by user in android ?

5条回答
  •  伪装坚强ぢ
    2021-01-30 05:01

    Here is a complete sample

    First define a zoom

        final CameraUpdate zoom = CameraUpdateFactory.zoomTo(5);
    

    Clear any map markers

        mMap.clear();
        //Declare a new marker
        final MarkerOptions mp = new MarkerOptions();
        //declare a EditText to get the user informed address
        EditText etEndereco = findViewById(R.id.map_prof_etEnd);
    
        Geocoder geocoder = new Geocoder(map_prof.this);
        List
    addresses = null; try { addresses = geocoder.getFromLocationName(etEndereco.getText().toString(), 1); } catch (IOException e) { e.printStackTrace(); } if (addresses.size() > 0) { double latitude = addresses.get(0).getLatitude(); double longitude = addresses.get(0).getLongitude(); mp.position(new LatLng(latitude, longitude)); Log.e("teste map2", "inserted latitude " + Latitude + ", inserted Longitude " + Longitude); mp.title("Selected location"); CameraUpdate center = CameraUpdateFactory.newLatLng(new LatLng(latitude, longitude)); mMap.clear(); mMap.addMarker(mp); mMap.moveCamera(center); mMap.animateCamera(zoom); }

提交回复
热议问题