1-) Android RMOS - haritalar google maps
1-) Android RMOS - build.gradle (Module:app)
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.navigation:navigation-fragment:2.0.0'
implementation 'androidx.navigation:navigation-ui:2.0.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.google.android.gms:play-services-maps:17.0.0'
implementation 'com.google.android.libraries.places:places:2.2.0'
implementation 'com.google.android.gms:play-services-location:17.0.0'
}
2-) AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application .. > ...
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
</application>
2-) HomeFragment.java
package com.tm.tm_version1.ui.home;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.SearchView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.common.api.Status;
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.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.libraries.places.api.Places;
import com.google.android.libraries.places.api.model.Place;
import com.google.android.libraries.places.widget.Autocomplete;
import com.google.android.libraries.places.widget.AutocompleteActivity;
import com.google.android.libraries.places.widget.model.AutocompleteActivityMode;
import com.tm.tm_version1.MainActivity;
import com.tm.tm_version1.R;
import com.tm.tm_version1.model.haritaNesne;
import java.util.Arrays;
import java.util.List;
import androidx.annotation.NonNull;
import androidx.core.view.MenuItemCompat;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProviders;
import static android.app.Activity.RESULT_CANCELED;
import static android.app.Activity.RESULT_OK;
public class HomeFragment extends Fragment implements OnMapReadyCallback, GoogleMap.OnMarkerClickListener {
SearchView searchView;
MenuItem searchItem;
private HomeViewModel homeViewModel;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
setHasOptionsMenu(true); // Arama cubugunu getirir
homeViewModel = ViewModelProviders.of(this).get(HomeViewModel.class);
View root = inflater.inflate(R.layout.fragment_home, container, false);
SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
search();
return root;
}
Menu menu;
@Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
this.menu = menu;
inflater.inflate(R.menu.map_search, menu);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.action_search:
Toast.makeText(getActivity(), "search", Toast.LENGTH_SHORT).show();
List<Place.Field> fields = Arrays.asList(Place.Field.ID, Place.Field.NAME, Place.Field.LAT_LNG);
Intent intent = new Autocomplete.IntentBuilder(
AutocompleteActivityMode.FULLSCREEN, fields)
.build(getActivity());
startActivityForResult(intent, AUTOCOMPLETE_REQUEST_CODE);
return true;
case R.id.action_settings:
Toast.makeText(getActivity(), "setting", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == AUTOCOMPLETE_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
Place place = Autocomplete.getPlaceFromIntent(data);
Toast.makeText(getActivity(), "" + "Place: " + place.getName() + ", " + place.getId(), Toast.LENGTH_SHORT).show();
placeAc(place);
((MainActivity)getActivity()).getSupportActionBar().setTitle(place.getName());
} else if (resultCode == AutocompleteActivity.RESULT_ERROR) {
// TODO: Handle the error.
Status status = Autocomplete.getStatusFromIntent(data);
Log.i(TAG, status.getStatusMessage());
} else if (resultCode == RESULT_CANCELED) {
// The user canceled the operation.
}
}
}
public void placeAc(final Place place) {
placess = place;
Toast.makeText(getActivity().getApplicationContext(), place.getLatLng() + " -> " + "Place: " + place.getName() + ", " + place.getId(), Toast.LENGTH_LONG).show();
if (place.getLatLng() != null) {
double latitude = place.getLatLng().latitude;
double longitude = place.getLatLng().longitude;
String name = place.getName();
// Creating a marker
final MarkerOptions markerOptions = new MarkerOptions();
Toast.makeText(getActivity().getApplicationContext(), String.valueOf(place.getLatLng()), Toast.LENGTH_LONG).show();
// Setting the position for the marker
markerOptions.position(place.getLatLng());
// Setting the title for the marker.
// This will be displayed on taping the marker
markerOptions.title(name);
mMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
@Override
public void onMapLoaded() {
// Animating to the touched position
mMap.moveCamera(CameraUpdateFactory.newLatLng(place.getLatLng()));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(place.getLatLng(), 13.3f));
// Placing a marker on the touched position
haritaNesne seciliNesne = new haritaNesne("1", "" + place.getName(), "" + place.getName());
mMap.addMarker(markerOptions).setTag(seciliNesne);
}
});
}
// List<Place.Field> fields = Arrays.asList(Place.Field.ID, Place.Field.NAME);
// Intent intent = new Autocomplete.IntentBuilder(
// AutocompleteActivityMode.FULLSCREEN, fields)
// .build(MapsActivity.this);
// startActivityForResult(intent, AUTOCOMPLETE_REQUEST_CODE);
}
public static final LatLng konumBaslangic = new LatLng(41.186565, 28.738528);
public static final LatLng konumBitis = new LatLng(41.260166, 28.742938); //41.260166, 28.742938
public static String TAG = "MapsActivity";
private GoogleMap mMap;
Place placess;
int AUTOCOMPLETE_REQUEST_CODE = 1;
public void search() {
String apiKey = getString(R.string.google_maps_key);
if (!Places.isInitialized()) {
Places.initialize(getActivity().getApplicationContext(), apiKey);
}
/*
final PlacesClient placesClient = Places.createClient(getActivity());
AutocompleteSupportFragment autocompleteFragment = (AutocompleteSupportFragment)
getChildFragmentManager().findFragmentById(R.id.autocomplete_fragment); //autocompletefragment nesnesi oluşturur
autocompleteFragment.getView().setBackgroundColor(Color.WHITE);
autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME, Place.Field.LAT_LNG));//döndürülecek yer verisi türlerini belirtir
autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() { //selected olayı gerçekleştiğinde kod bloğu çalıştırmaya yarar
@Override
public void onPlaceSelected(@NonNull final Place place) {//selected işlemi gerçekleşince çalışacak kod bloğu
placeAc(place);
}
@Override
public void onError(Status status) { //hata oluştuğunda çalışacak kod bloğu
Toast.makeText(getActivity().getApplicationContext(), "An error occurred: " + status, Toast.LENGTH_LONG).show();
}
});*/
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.getUiSettings().setZoomControlsEnabled(false);
mMap.getUiSettings().setCompassEnabled(true);
mMap.setPadding(0, 0, 0, 100);
haritaNesne sefer1 = new haritaNesne("0551 147 2017", "İSTANBUL HAVAALANINDAN", "İSTANBUL ARNAVUTKÖYE");
haritaNesne sefer2 = new haritaNesne("0555 140 2000", "İSTANBUL ARNAVUTKÖYDEN", "İSTANBUL HAVAALANINA");
MarkerOptions marker1 = new MarkerOptions().position(konumBaslangic)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.taxi));
Marker marker = mMap.addMarker(marker1);
marker.setTag(sefer2);
marker.showInfoWindow();
MarkerOptions marker12 = new MarkerOptions().position(konumBitis)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.taxi));
Marker marker3 = mMap.addMarker(marker12);
marker3.setTag(sefer1);
marker3.showInfoWindow();
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(konumBaslangic, 12)); // küçüldükçe yukarıdan gösterir
mMap.setOnMarkerClickListener(this);
mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
@Override
public View getInfoWindow(Marker arg0) {
return null;
}
@Override
public View getInfoContents(Marker arg0) {
View v = null;
try {
haritaNesne nesne = (haritaNesne) arg0.getTag();
v = getLayoutInflater().inflate(R.layout.custom_infowindow, null);
if (nesne.tel.equals("1")) { // eğer teli 1 ise kendi konumumdur
LinearLayout linerTik = (LinearLayout) v.findViewById(R.id.linerTik);
linerTik.setVisibility(View.GONE);
}
TextView addressTxt = (TextView) v.findViewById(R.id.nereden);
addressTxt.setText(nesne.nereden);
TextView nameTxt = (TextView) v.findViewById(R.id.nereye);
nameTxt.setText(nesne.nereye);
TextView mobileTxt = (TextView) v.findViewById(R.id.tel);
mobileTxt.setText(nesne.tel);
} catch (Exception ev) {
System.out.print(ev.getMessage());
}
return v;
}
});
}
@Override
public boolean onMarkerClick(Marker marker) {
haritaNesne nesne = (haritaNesne) marker.getTag();
Toast.makeText(getActivity(), "Tel : " + nesne.tel + " Nereden : " + nesne.nereden, Toast.LENGTH_SHORT).show();
return false;
}
}