Merhaba bu yazimda Google Maps kullanimini bir ornekle anlatacagim.
Bu projede yapacagimiz seyleri kisaca anlatmak gerekirse : Oncelikle bir edittext e nerenin konumunu gormek istedigimizi girecegiz.
Ve yanindaki butona tikladigimizda haritada orayi bulup harita uzerinde marker ile bize gosterecek.
Altta ise harita gorunumunu degistirebilecegimiz buton olacak. Uydu gorunumu ise normal gorunume cevirebilecegiz, normal ise
uydu gorunume.
En sag altta ise haritayi yakinlastirip uzaklastirabilecegimiz butonlar yer alacak.
Simdilik uygulamamiz calistiginda nasil bir gorunumu olacagini asagidaki ekran resimleri uzerinden bakabilirsiniz.
Oncelikle android studio da new project dedikten sonra activity olarak Google Maps activity i seciyoruz. Bu sekilde acitivity_maps.xml
de map tnaimli olarak geliyor.
Simdi ise res/values altinda yer alan google_maps_api.xml da 7. satirdaki linki tarayicimizda aciyoruz. Buradan Api anahtari olusturup
satir 23 de yer alan YOUR KEY HERE kismina yapistiriyoruz. Bunu yaptigimizda keyimiz otomatik olarak Android Manifest e de eklenmis olacak.
Cunku ikisinde de ayni key olan google_maps_key yer aliyor.
Simdi ise kodlamaya gecelim.
Buradaki kodlamamizin mantigi su sekilde olacak. Edittext e girilen degeri alip onun string karsiligini bulacagiz.
Daha sonra girilen deger null degilse veya bos degilse o halde Geocoder classindan isim ile enlem boylam bulan
getFromLocationName methoduna input verecegiz. Bu sekilde girilen degerin enlem boylam karsiligini bulacagiz. Ve map te
o yere marker ekleyip kamerayi oraya yaklastiracagiz.
activity_maps.xml
MapsActivity.java
package com.example.gamze.googlemapsex02;
import android.location.Address;
import android.location.Geocoder;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ZoomControls;
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;
import java.io.IOException;
import java.util.List;
public class MapsActivity2 extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private final static int REQUEST_LOCATION=90;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps2);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
final Button uyduBtn = (Button) findViewById(R.id.uydu);
uyduBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(mMap.getMapType() == GoogleMap.MAP_TYPE_NORMAL){
mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
uyduBtn.setText("NORMAL");
}else{
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
uyduBtn.setText("UYDU");
}
}
});
ZoomControls zoomControls = (ZoomControls) findViewById(R.id.zoomControl);
zoomControls.setOnZoomInClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mMap.animateCamera(CameraUpdateFactory.zoomIn());
}
});
zoomControls.setOnZoomOutClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mMap.animateCamera(CameraUpdateFactory.zoomOut());
}
});
final EditText gidilecekYer = (EditText) findViewById(R.id.gidilecekYer);
Button bulBtn = (Button) findViewById(R.id.bul);
bulBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String gidilecekYerAdi = gidilecekYer.getText().toString();
if (gidilecekYerAdi != null && !gidilecekYerAdi.equals("")){
Geocoder geocoder = new Geocoder(MapsActivity2.this);
List
addressList = null;
try{
addressList = geocoder.getFromLocationName(gidilecekYerAdi, 1);
}catch (IOException ex){
ex.printStackTrace();
}
Address address = addressList.get(0);
LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());
mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.addMarker(new MarkerOptions().position(latLng).title("Burasi: "+ gidilecekYerAdi));
}
}
});
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney 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));
}
}