AndroidStudio Google Maps (launch map by using Button)

0

Good to all, I'm new to Android and I'm testing the Google API, to practice it occurred to me to create a main menu with a button, that by pressing this I launched a map with specific coordinates.

Test the api from main activity and it works correctly.

I am currently uploading the main layout but clicking the button does not launch the map. What you want is to pass the parameters to another class and that this class manages the parameters and presents the map. Here the code:

MainActivity:

package company.project.phk.whereisit;
import android.app.Dialog;
import android.app.FragmentTransaction;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;


import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.common.api.GoogleApiActivity;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;


public class MainActivity extends AppCompatActivity{

    public GoogleMap mGoogle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        TextView TV1 = (TextView) findViewById(R.id.PraderaColor);
        TV1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                mapClass miMapa= new mapClass();
                miMapa.setDatos(14.8499202,-91.5375707,"Mi Ubicacion");

            }
        });

    }


}

MainActivity xml

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity"
    >

    <TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Miubicacioin"
    android:textAlignment="center"

    />
    </LinearLayout>

mapClass: in this class I intend to receive the parameters and launch the map.

package company.project.phk.whereisit;

import android.app.Dialog;
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;


public class mapClass  extends FragmentActivity implements  OnMapReadyCallback {

    public GoogleMap mGoogle;
    public Double Coordenada1;
    public Double Coordenada2;
    public String Messaje;
    public LatLng miUbicacion;



    public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
        setContentView(R.layout.mapalayout);

     MapFragment mapF = (MapFragment) getFragmentManager().findFragmentById(R.id.mapa);
        mapF.getMapAsync(mapClass.this);

   }

    public void setDatos( Double c1, Double c2, String m )
    {
        Coordenada1=c1;
        Coordenada2=c2;
        Messaje=m;
    }

    public void onMapReady(GoogleMap googleMap) {

      //  mGoogle = googleMap;
        miUbicacion = new LatLng(Coordenada1, Coordenada2);
        googleMap.addMarker(new MarkerOptions().position(miUbicacion).title(Messaje));
       googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(miUbicacion, 16));


    }

}

mapLayout xml

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/mapa"
android:name="com.google.android.gms.maps.MapFragment"/>

First of all thank you, for taking the time and checking my question.

How to make the map execute correctly from the button?

    
asked by Boris Fuentes 12.05.2017 в 05:39
source

1 answer

0

That's not the way to pass parameters between activities / fragments Tip: link

I advise you to read some theory about how the activities and the fragments work, the communication between them and then try again this example you are doing.

    
answered by 12.05.2017 / 10:17
source