2016-10-19 65 views
-1

請幫助我,我創建了一個類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(); 
     } 

    } 

我想使用谷歌地圖添加位置。請有任何想法嗎?

+0

我們不能幫助您,直到您與我們分享您的代碼 –

+0

我編輯了我的出版物,您現在可以找到我的代碼。我想添加谷歌地圖的位置,並將其保存在數據庫中 – salma

+0

您應該從這裏開始:https://developers.google.com/maps/documentation/android-api/start,因爲您必須先實施谷歌地圖,然後才能獲得來自用戶的位置。 –

回答

1

Google爲開發人員提供了一個非常簡潔的指南,可以開始使用適用於Android的Maps API。

看看here用於生成所需的API密鑰。

不要忘記,以獲得API密鑰或它不會工作

這裏是XML爲地圖

<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" /> 

片段,這裏是用於獲取初學者片段開始

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)); 
    } 
} 

希望這有助於

+0

@salma你能解決你的問題嗎? – HenriqueMS