Googlemap move camera from map to certain location

Asked

Viewed 63 times

1

I added the map to Fragment, it is already appearing, I even added a random marker with the map starting on it. the problem is that now I am using the Geocoder.

I put the address in edittext and it converts into Lng and Lat, so far so good, it is converting and saving in the bank.

What I want now is that by typing the name London (for example) and pressing the FIND button, it changes the camera from the map to the lat and lng from London.

The mistake you’re making is java.lang.NullPointerException.

MapsActivity mapa = new MapsActivity();
    getSupportFragmentManager().beginTransaction().replace(R.id.fragLayout, mapa).commit();

Geocoder

String stringLocal = localizacao.getText().toString();
    //Convertendo endereço para coordenada

    Geocoder geocoder = new Geocoder(this, Locale.getDefault());
    List<Address> addresses = null;
    try {
        addresses = geocoder.getFromLocationName(stringLocal, 1);
    } catch (IOException e) {
        e.printStackTrace();
    }
    Address address = addresses.get(0);
    double longitude = address.getLongitude();
    double latitude = address.getLatitude();

Boot code to find.

 if (view == ACHAR) {
        LatLng latLng = new LatLng(latitude, longitude);
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 20));
        map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
    }

Mapsfragment

public class MapsFragment extends BaseFragment implements OnMapReadyCallback {
private GoogleMap mMap;
@Override
public View onCreateView(LayoutInflater inflate, ViewGroup container, Bundle savedInstanceState) {
    super .onCreate(savedInstanceState);
    View view = inflate.inflate(R.layout.activity_maps, container, false);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
    return view;
}
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    // Add a marker in Sydney and move the camera
    LatLng sydney = new LatLng(-34, 151);
    mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}

}

  • Check whether the variables latitude and longitude, used in the code of the FIND button, are the same that are initialized in Geocoder.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.