将MapView添加到我的活动中

时间:2016-10-19 12:02:51

标签: android mysql google-maps-android-api-2

请帮助我,我创建了一个班级newReclamation,我们可以在其中使用MapView添加回收名称,类型,说明和位置。如何添加MapView并使用Marker手动标记我的位置,然后在mysql数据库中保存(Lang,Lat)。

PS:现在我只保存回收名称,类型和描述,但它确实有效。请帮我。

public class NewReclamationActivity extends Activity implements OnItemSelectedListener {

    // Progress Dialog
    private ProgressDialog pDialog;
    private SQLiteHandler db;

    JSONParser jsonParser = new JSONParser();
    EditText inputName;
    EditText inputDesc;
    Spinner spinner;



    // LogCat tag
    private static final String TAG = NewReclamationActivity.class.getSimpleName();
    // JSON Node names
    private static final String TAG_SUCCESS = "success";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_reclamation);

        spinner = (Spinner) findViewById(R.id.spinner);
        spinner.setOnItemSelectedListener(this);
// Create an ArrayAdapter using the string array and a default spinner layout
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                R.array.type_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
        spinner.setAdapter(adapter);

        // Edit Text
        inputName = (EditText) findViewById(R.id.inputName);
        inputDesc = (EditText) findViewById(R.id.inputDesc);

        // SqLite database handler
        db = new SQLiteHandler(getApplicationContext());



        // Create button
        Button btnCreateReclamation = (Button) findViewById(R.id.btnCreateReclamation);

        // button click event
        btnCreateReclamation.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                // creating new reclamation in background thread
                new CreateNewReclamation().execute();
            }
        });

    }

    public void onItemSelected(AdapterView<?> parent, View view,
                               int pos, long id) {
        // An item was selected. You can retrieve the selected item using
        // parent.getItemAtPosition(pos)
    }

    public void onNothingSelected(AdapterView<?> parent) {
        // Another interface callback
    }


    /**
     * Background Async Task to Create new reclamation
     * */
    class CreateNewReclamation extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(NewReclamationActivity.this);
            pDialog.setMessage("Creating Reclamation..");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();

        }




        /**
         * Creating reclamation
         * */

        // Fetching user details from SQLite
        HashMap<String, String> user = db.getUserDetails();
        String user_id=user.get("uid");


        String name = inputName.getText().toString();
        String type = spinner.getSelectedItem().toString();
        String description = inputDesc.getText().toString();


        protected String doInBackground(String... args) {


            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("name", name));
            params.add(new BasicNameValuePair("type", type));
            params.add(new BasicNameValuePair("description", description));
            params.add(new BasicNameValuePair("user_id",user_id));

            // getting JSON Object
            // Note that create product url accepts POST method
            JSONObject json = jsonParser.makeHttpRequest(AppConfig.url_create_reclamation,
                    "POST", params);

            // check log cat fro response
            Log.d("Create Response", json.toString());

            // check for success tag
            try {
                int success = json.getInt(TAG_SUCCESS);

                if (success == 1) {
                    // successfully created reclamation
                    Intent i = new Intent(getApplicationContext(), MainActivity.class);
                    startActivity(i);

                    // closing this screen
                    finish();
                } else {
                    // failed to create reclamation
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) {
            // dismiss the dialog once done
            pDialog.dismiss();
        }

    }

我想使用谷歌地图添加位置。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

Google has provided a really neat guide for developers to get started with the Maps API for android.

Take a look here for generating the required API key.

Don't forget to get the API key or it won't work

Here is the XML for a fragment for a map

<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="match_parent"
android:id="@+id/map"
tools:context=".MapsActivity"
android:name="com.google.android.gms.maps.SupportMapFragment" />

and here is the beginner snippet for getting started

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
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 MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        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));
    }
}

Hope this helps