Daha önce Google Map’i Android uygulamasına nasıl yerleştirebileceğimizi anlatmıştık. Buradan ulaşabilirsiniz. Şimdi de Google Map içerisinde konum aramayı anlatacağım. Bunun için Asyntask yapısını bilmeniz gerekmektedir. Asyntask ile ilgili yazıya da buradan ulaşabilirsiniz.
AsyncTask ve Google Map’i öğrendik. Şimdi de harita içerisinde konum arayalım.
activity_main.xml dosyası örnek teşkil etmesi açısından aşağıdaki gibidir.
<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" > <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/btn_find" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Bul" android:layout_alignParentRight="true"/> <EditText android:id="@+id/et_location" android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="text" android:hint="@string/hnt_et_location" android:layout_toLeftOf="@id/btn_find"/> </RelativeLayout> <fragment android:id="@+id/map" android:layout_width="match_parent" android:layout_height="match_parent" android:name="com.google.android.gms.maps.SupportMapFragment" /> </LinearLayout>
MainActivity.java dosyamızdaki Java kodlarımıza bakalım;
package eee.eee; import java.io.IOException; import java.util.List; import android.location.Address; import android.location.Geocoder; import android.os.AsyncTask; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import com.google.android.gms.maps.CameraUpdateFactory; import com.google.android.gms.maps.GoogleMap; 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 FragmentActivity { GoogleMap googleMap; MarkerOptions markerOptions; LatLng latLng; @Override protectedvoid onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); SupportMapFragment supportMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); googleMap = supportMapFragment.getMap(); //Yer göster/gizle googleMap.setMyLocationEnabled(true); // Zoom kontrollerini aktif/pasif yap googleMap.getUiSettings().setZoomControlsEnabled(true); Button btn_find = (Button) findViewById(R.id.btn_find); OnClickListener findClickListener = new OnClickListener() { @Override public void onClick(View v) { EditText etLocation = (EditText) findViewById(R.id.et_location); //Konumu sadece Türkiye içerisinde araması için "Türkiye" String location = "Türkiye,"+etLocation.getText().toString(); if(location!=null && !location.equals("")){ new GeocoderTask().execute(location); } }}; btn_find.setOnClickListener(findClickListener); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } // Geocode web servisi için Asynctask privateclass GeocoderTask extends AsyncTask<String, Void, List<Address>>{ @Override protected List<Address> doInBackground(String... locationName) { // Geocoder'dan bir nesne türetilir. Geocoder geocoder = new Geocoder(getBaseContext()); List<Address> addresses = null; try { // Dönen ilk 3 adres alınır.. Diziye aktarılır addresses = geocoder.getFromLocationName(locationName[0], 3); } catch (IOException e) { e.printStackTrace(); } returnaddresses; } @Override protectedvoid onPostExecute(List<Address> addresses) { if(addresses==null || addresses.size()==0){ Toast.makeText(getBaseContext(), "Konum Bulunamadı.", Toast.LENGTH_SHORT).show(); } // Haritadaki daha önceki marker'ları temizle googleMap.clear(); // dizideki konumları marker olarak haritaya ekle for(inti=0;i<addresses.size();i++){ Address address = (Address) addresses.get(i); // latLng olarak al latLng = new LatLng(address.getLatitude(), address.getLongitude()); String addressText = String.format("%s, %s",address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",address.getCountryName()); markerOptions = new MarkerOptions(); markerOptions.position(latLng); markerOptions.title(addressText); googleMap.addMarker(markerOptions); // Dizideki ilk konuma odaklan. Zoom 11 if(i==0) googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng)); googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 11)); ); } } } }