Bu projemiz de MapsActivity kullanarak haritadan konum işaretleme ve seçilen konumlar arasında çizgi çizmeyi öğreneceğiz.
Google Maps Activity seçerek projemizi oluşturuyoruz.
Kodları açıklamaya geçmeden projemin son halinini, aklınızda daha kolay canlanması için aşağıya ekliyorum.
Tasarımım
activity_maps.xml kodum;
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content" android:layout_height="fill_parent" tools:context=".MapsActivity" android:background="@color/colorBack"> <LinearLayout android:id="@+id/layout_top" android:orientation="vertical" android:layout_width="438dp" android:layout_height="450dp"> <fragment android:id="@+id/map" android:name="com.google.android.gms.maps.SupportMapFragment" android:layout_width="438dp" android:layout_height="450dp" /> </LinearLayout> <LinearLayout android:id="@+id/layout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/layout_top" android:layout_marginTop="9dp" android:layout_weight="1" android:orientation="horizontal" android:padding="10dp"> <TextView android:id="@+id/tv_lat" android:layout_width="50dp" android:layout_height="50dp" android:layout_gravity="center" android:text="Lat" /> <EditText android:id="@+id/edt_lat" android:layout_width="150dp" android:layout_height="match_parent" android:layout_gravity="center" android:hint="41.025629" /> <TextView android:id="@+id/tv_lng" android:layout_width="50dp" android:layout_height="50dp" android:layout_gravity="center" android:text="Lng" /> <EditText android:id="@+id/edt_lng" android:layout_width="150dp" android:layout_height="match_parent" android:layout_gravity="center" android:layout_weight="1" android:hint="28.974138"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/layout" android:padding="8dp" > <Button android:id="@+id/btn_ekle" android:layout_width="410dp" android:layout_height="70dp" android:text="Ekle" android:layout_gravity="center" android:background="@color/colorEkle"/> </LinearLayout> </RelativeLayout> ------------------------------------------------ En dışta bir Relative Layout oluşturup, altına bir Linear Layout oluşturdum ve fragment yerleştirdim.Linear Layoutumu sonlandırıp yeni bir Linear Layout açtım ve bu layoutumun içinde de TextView ve Edittextlerimi yerleştirerek Linear Layoutumu sonlandırdım. Son olarak yeni bir Linear Layout oluşturarak içine butonumu yerleştirdim. -------------------------------------------------
MapsActivity.java
public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback { private GoogleMap mMap; ArrayList<Marker> markers = new ArrayList<>(); private final static int REQUEST_lOCATION = 90; EditText edtLat, edtLng; Button button; String lat, lng; Double latitude, longitude;
@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 ); edtLng = (EditText) findViewById( R.id.edt_lng ); edtLat = (EditText) findViewById( R.id.edt_lat ); button = (Button) findViewById( R.id.btn_ekle ); button.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { lat = edtLat.getText().toString(); latitude = Double.valueOf( lat ).doubleValue(); lng = edtLng.getText().toString(); longitude = Double.valueOf( lng ).doubleValue(); LatLng latLng = new LatLng( latitude, longitude ); //nesne yaratıldı if (markers.size() > 0) { mMap.addPolyline( new PolylineOptions() .add( markers.get( markers.size() - 1 ).getPosition(), latLng ) .width( 8f ) .color( Color.RED ) ); /* Oluşturduğum her bir marker ile bir önceki marker arasına kırmızı çizgi çizmek istediğim için böyle bir if deyimi oluşturdum. */ } Marker marker = mMap.addMarker( new MarkerOptions().position( new LatLng( latitude, longitude ) ) ); markers.add( marker ); //marker ekliyoruz mMap.animateCamera( CameraUpdateFactory.newLatLng( latLng ) ); } } );
private boolean ShouldAddMarker(double latA, double lngA, double latB, double lngB) { Location locationA = new Location( "point A" ); locationA.setLatitude( latA ); locationA.setLongitude( lngA ); Location locationB = new Location( "point B" ); locationB.setLatitude( latB ); locationB.setLongitude( lngB ); float distance = locationA.distanceTo( locationB ); return distance > 1000; } @Override public void onMapReady(GoogleMap googleMap) { mMap = googleMap; if (ActivityCompat.checkSelfPermission( this, Manifest.permission.ACCESS_FINE_LOCATION ) == PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling mMap.setMyLocationEnabled( true ); } else { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { requestPermissions( new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_lOCATION ); } } } ---------------------------------------------------------------------- Github link: " github.com/yaseminuctas/MapActivity "