set bookmark in google maps android (API V2)

1

I have these two classes that call my map, and my activity that contains it, the issue is that I need to pass on the parent activity, the coordinates for the child activity, but I do not know how to do it, if I do a new method that go to the fragment, to fill the coordinates, do not assign them correctly, and another way I have no idea

  

MapViewFragment.java

public class MapViewFragment extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.location_fragment);
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    // Add a marker in Sydney, Australia, 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));
}

}

  

LocalActivity.java

    public class LocalActivity extends AppCompatActivity {
    private Locales local;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        final Context context = getContext();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_locales);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);

        int id;
        id = getIntent().getIntExtra("id",0);
        Servicio ser = new Servicio();
        local = new Locales();
        try {
            local = ser.GetLocal(context, id);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        toolbar.setTitle(local.getNombre());
    }
 }

Then I have my xml

  

content_locales.xml

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    **<fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="285dp"
        android:id="@+id/map"
        tools:context=".Fragment.MapViewFragment"
        android:name="com.google.android.gms.maps.SupportMapFragment" />**

    <Button
        android:id="@+id/btnReservar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/solicitar_reserva" />
</LinearLayout>

    
asked by Pablo Ezequiel Ferreyra 16.05.2018 в 06:49
source

2 answers

0

In this case you should check what is the Activity that actually adds or replaces the Fragment MapViewFragment , to send the geo location data from the Activity that performs the transaction of Fragments you can make it from this way, creating a bundle and adding it using setArguments () that provides arguments that will be preserved through the destruction and creation of fragments:

FragmentManager fragmentManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

            //Crea bundle.
            Bundle bundle = new Bundle();
            bundle.putDouble("latitud", 47.1584549);
            bundle.putDouble("longitud", 27.601441799999975);

            //agrega argumentos al Fragment
            MapViewFragment mapFragment = new MapViewFragment();
            mapFragment.setArguments(bundle);

           //Realiza la transacción.
            fragmentManager = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction;
            fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.frameLayout,mapFragment).commit();

  }

To receive the values within Fragment , it is done in the following way:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
 ...
 ...
 Bundle bundle = this.getArguments(); //Obtiene los argumentos.

        if(bundle != null) {
            //Obtiene los valores definidos por la key.
            Double latitud = bundle.getDouble("latitud");
            Double longitud = bundle.getDouble("longitud");         
        }
...
}
    
answered by 16.05.2018 / 19:46
source
1

You can directly put the coordinates in MapViewFragment.java and thus set the markers. An example, modify your code in onMapReady :

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    Farmas(googleMap);

    mMap.setMyLocationEnabled(true);                  
    mMap.getUiSettings().setMyLocationButtonEnabled(true);  // agrega el botón de localización
    mMap.getUiSettings().setZoomControlsEnabled(true);      // agrega los botones del zoom (+ -)
}

public void Farmas(GoogleMap googleMap) {
    mMap = googleMap;
    float zoomLevel = 13;      // el nivel del zoom con el cual inicia el mapa

    final  LatLng farma1 = new LatLng(25.6755027,-100.2678697);  // las coordenadas (latitud, longitud) que lo agregas en position
    final  LatLng farma2 = new LatLng(25.672138,-100.2514227);
     // los marcadores (posición, título, ícono):
    mMap.addMarker(new MarkerOptions().position(farma1).title("Farmacias Guadalajara 'Centro'").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));
    mMap.addMarker(new MarkerOptions().position(farma2).title("Farmacias Guadalajara 'La Pastora'").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(farma1, zoomLevel));  // para mostrar el mapa con zoom, en este caso nivel 13
}

If you are going to get the coordinates in another fragment , for example through EditText , then apply the solution that gives @Jorgesys.

    
answered by 16.05.2018 в 23:28