Google Maps API,Google’ın android uygulamalrı için bize sunduğu apilerdir.
Yapacağımız uygulamada Fragment içerisine Google Map Configürasyonu ekliyeceğiz.
ilk olarak erişim izinlerini (Manifest ) ve kütüphaneleri (gradle) dahil etmemiz gerekiyor.
implementation 'com.google.android.gms:play-services-maps:15.0.1'
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
values>google_maps_api.xml oluşturuyoruz ve bunu içerisine Manifest kısımında oluşturduğumuz string ifadeye Google Cloud Platform’dan almış olduğumuz API’yi dahil ediyoruz.
<resources> <!-- TODO: Before you run your application, you need a Google Maps API key. To get one, follow this link, follow the directions and press "Create" at the end: https://console.developers.google.com/flows/enableapi?apiid=maps_android_backend&keyType=CLIENT_SIDE_ANDROID&r=31:86:12:0C:DA:C7:2F:CA:6D:2F:97:AB:C6:5E:0A:63:82:1E:3D:BF%3Bcom.example.googlemap2 You can also add your credentials to an existing key, using these values: Package name: 31:86:12:0C:DA:C7:2F:CA:6D:2F:97:AB:C6:5E:0A:63:82:1E:3D:BF SHA-1 certificate fingerprint: 31:86:12:0C:DA:C7:2F:CA:6D:2F:97:AB:C6:5E:0A:63:82:1E:3D:BF Alternatively, follow the directions here: https://developers.google.com/maps/documentation/android/start#get-key Once you have your key (it starts with "AIza"), replace the "google_maps_key" string in this file. --> <string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">Your Here Key</string> </resources>
Yeni boş bir Activity oluşturalım,Bu boş Activity içerisinde işlemlerimizi sürdüreceğiz.
File>New>New Project>Empty Activity
Daha sonra bir Fragment(GoogleMapFragment) oluşturalım.Bu Fragment içerisinde Google Map API işlemlerini yapıcağız.
Bu Fragmenti Activity içerisinde görebilmemiz için Activity_main.xml kısımında bir FrameLayout oluştururuz.
<FrameLayout android:id="@+id/frame" android:layout_width="match_parent" android:layout_height="match_parent"/>
Vermiş olduğumuz id’yi tanımlayabilmemiz için yeni bir sınıf(ChangeFragment) oluşturup içerisine bir method yazıp aktif hale getirmeliyiz.
public class ChangeFragment {
Context context;
public ChangeFragment(Context context) {
this.context = context;
}
public void change(Fragment fragment){
((FragmentActivity) context).getSupportFragmentManager()
.beginTransaction()
.replace(R.id.frame,fragment,"fragment")
.setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
.commit();
}
}
ChangeFragment Classımızda oluşturmuş olduğumuz methodu MainActivity.java classımızda nesne üreterek ulaşmış oluruz .Böylelikle tek bir Class üzerinden işlem yapmadığımız için yapmış olduğumuz uygulama daha hızlı ve dinamik bir şekilde çalışmış olur.
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ChangeFragment changeFragment=new ChangeFragment(MainActivity.this);
changeFragment.change(new GoogleMapFragment());
}
}
fragment_google_map.xml kısımdan MapView’mızı oluşturuyoruz.
<com.google.android.gms.maps.MapView android:id="@+id/map" android:name="com.google.android.gsm.maps.SupportMapFragment" android:layout_width="match_parent" android:layout_height="match_parent"> </com.google.android.gms.maps.MapView>
Bu işlemide tamamladığımıza göre GoogleMapFragmentt Classımızda MapView’mızı tanımlayalım ve GoogleMap için gerekli işlemleri yapabilmek için OnMapReadyCallBack implements etmemiz gerekir.
public class GoogleMapFragment extends Fragment implements OnMapReadyCallback { View view; MapView mapView; GoogleMap mMap; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { view = inflater.inflate(R.layout.fragment_google_map2, container, false); mapView = view.findViewById(R.id.map); mapView.onCreate(savedInstanceState); mapView.onResume(); mapView.getMapAsync(GoogleMapFragment.this); return view; } @Override public void onMapReady(GoogleMap googleMap) { mMap = googleMap; LatLng latLng = new LatLng(40.9875, 28.993); mMap.addMarker(new MarkerOptions().position(latLng).title("marker")); } }
Çok beğendim işimi aşırı kolaylaştırdı mükemmel bir iş cikarilmis 👏👏👏👏👏👏