Google Play xizmatlari
Google Repository
bitta Play Services xizmatiga bog'liqliklarni loyihamizning build.gradle fayliga qo'shishimiz kerak
Odatiy bo'lib, loyiha tarkibida ikkita build.gradle fayli mavjud; bittasi modul papkasida (quyidagi rasmda 1 raqami bilan ko'rsatilgan) va bittasi loyiha papkasida, yuqori sathida (quyidagi rasmdagi 2 raqami bilan). Odatiy bo'lib, loyiha tarkibida ikkita build.gradle fayli mavjud Faylni modul papkasida, yuqoridagi rasmda 1 raqami ostida oching. Google Play Services kutubxonasining so'nggi versiyasini "bog'liqliklar" bo'limiga qo'shing. Yozish vaqtiga ko'ra, bu 7.5.0. Buning so'nggi versiyasini va boshqa ko'plab mashhur kutubxonalarni "Turli xil" bo'limdagi foydali havolalar sahifasidagi havola orqali bilib olishingiz mumkin.
package ...
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity; import android.util.Log;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory; import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.CameraPosition; import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapActivity extends AppCompatActivity { GoogleMap googleMap;
//координаты для маркера
private static final double TARGET_LATITUDE = 17.893366; private static final double TARGET_LONGITUDE = 19.511868;
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_map); createMapView();
addMarker();
//добавляем на карту свое местоположение googleMap.setMyLocationEnabled(true);
}
//создаем карту
private void createMapView(){
try {
if(null == googleMap){
googleMap=((MapFragment) getFragmentManager().findFragmentById( R.id.mapView)).getMap();
if(null == googleMap) { Toast.makeText(getApplicationContext(),
"Error creating map",Toast.LENGTH_SHORT).show();
}
}
} catch (NullPointerException exception){ Log.e("mapApp", exception.toString());
}
}
//добавляем маркер на карту private void addMarker(){
double lat = TARGET_LATITUDE; double lng = TARGET_LONGITUDE;
//устанавливаем позицию и масштаб отображения карты CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(lat, lng))
.zoom(15)
.build(); CameraUpdate cameraUpdate=
CameraUpdateFactory.newCameraPosition(cameraPosition); googleMap.animateCamera(cameraUpdate);
if(null != googleMap){
googleMap.addMarker(new MarkerOptions()
.position(new LatLng(lat, lng))
.title("Mark")
.draggable(false)
);
}
}
}
|