Je veux montrer une carte de mon activité.
Dans google map V1, nous utilisons -
<com.google.Android.maps.MapView
Android:id="@+id/mapview"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
Android:apiKey="@string/api_map_key"
Android:clickable="true"
Android:enabled="true" />
et étendez l'activité en utilisant la classe MapActivity.
Dans Versing 2, il utilise Fragment au lieu de mapview et doit étendre l'activité de FragmentActivity à la place de Normal. ex-
<fragment
Android:id="@+id/map"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
class="com.google.Android.gms.maps.SupportMapFragment" />
Maintenant, puis-je utiliser la même façon de créer mapview au lieu de Fragment avec la version 2. ()
Quelqu'un peut-il utiliser MapView avec V2?
oui, vous pouvez utiliser MapView en v2 ... pour plus de détails, vous pouvez obtenir de l'aide
https://Gist.github.com/joshdholtz/4522551
SomeFragment.Java
public class SomeFragment extends Fragment implements OnMapReadyCallback{
MapView mapView;
GoogleMap map;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.some_layout, container, false);
// Gets the MapView from the XML layout and creates it
mapView = (MapView) v.findViewById(R.id.mapview);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);
return v;
}
@Override
public void onMapReady(GoogleMap googleMap) {
map = googleMap;
map.getUiSettings().setMyLocationButtonEnabled(false);
map.setMyLocationEnabled(true);
/*
//in old Api Needs to call MapsInitializer before doing any CameraUpdateFactory call
try {
MapsInitializer.initialize(this.getActivity());
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
*/
// Updates the location and zoom of the MapView
/*CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(43.1, -87.9), 10);
map.animateCamera(cameraUpdate);*/
map.moveCamera(CameraUpdateFactory.newLatLng(43.1, -87.9));
}
@Override
public void onResume() {
mapView.onResume();
super.onResume();
}
@Override
public void onPause() {
super.onPause();
mapView.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
}
AndroidManifest.xml
<manifest xmlns:Android="http://schemas.Android.com/apk/res/Android"
package="com.example"
Android:versionCode="1"
Android:versionName="1.0" >
<uses-sdk
Android:minSdkVersion="8"
Android:targetSdkVersion="15" />
<uses-permission Android:name="Android.permission.INTERNET"/>
<uses-permission Android:name="com.google.Android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission Android:name="Android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission Android:name="Android.permission.ACCESS_FINE_LOCATION"/>
<uses-feature
Android:glEsVersion="0x00020000"
Android:required="true"/>
<permission
Android:name="com.example.permission.MAPS_RECEIVE"
Android:protectionLevel="signature"/>
<uses-permission Android:name="com.example.permission.MAPS_RECEIVE"/>
<application
Android:icon="@drawable/ic_launcher"
Android:label="@string/app_name"
Android:theme="@style/AppTheme" >
<meta-data
Android:name="com.google.Android.maps.v2.API_KEY"
Android:value="your_key"/>
<activity
Android:name=".HomeActivity"
Android:label="@string/app_name" >
<intent-filter>
<action Android:name="Android.intent.action.MAIN" />
<category Android:name="Android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
some_layout.xml
<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
xmlns:tools="http://schemas.Android.com/tools"
Android:layout_width="fill_parent"
Android:layout_height="fill_parent" >
<com.google.Android.gms.maps.MapView Android:id="@+id/mapview"
Android:layout_width="fill_parent"
Android:layout_height="fill_parent" />
</LinearLayout>
Échantillon plus complet de ici et ici .
Ou vous pouvez consulter mon exemple de mise en page. p.s pas besoin de mettre la clé API dans la vue de la carte.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
Android:orientation="vertical"
Android:layout_width="match_parent"
Android:layout_height="match_parent">
<com.google.Android.gms.maps.MapView
Android:id="@+id/map_view"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
Android:layout_weight="2"
/>
<ListView Android:id="@+id/nearby_lv"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
Android:background="@color/white"
Android:layout_weight="1"
/>
</LinearLayout>
J'ai créé un échantillon factice pour Google Maps v2 Android avec Kotlin et AndroidX
Vous pouvez trouver le projet complet ici: github-link
MainActivity.kt
class MainActivity : AppCompatActivity() {
val position = LatLng(-33.920455, 18.466941)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
with(mapView) {
// Initialise the MapView
onCreate(null)
// Set the map ready callback to receive the GoogleMap object
getMapAsync{
MapsInitializer.initialize(applicationContext)
setMapLocation(it)
}
}
}
private fun setMapLocation(map : GoogleMap) {
with(map) {
moveCamera(CameraUpdateFactory.newLatLngZoom(position, 13f))
addMarker(MarkerOptions().position(position))
mapType = GoogleMap.MAP_TYPE_NORMAL
setOnMapClickListener {
Toast.makeText(this@MainActivity, "Clicked on map", Toast.LENGTH_SHORT).show()
}
}
}
override fun onResume() {
super.onResume()
mapView.onResume()
}
override fun onPause() {
super.onPause()
mapView.onPause()
}
override fun onDestroy() {
super.onDestroy()
mapView.onDestroy()
}
override fun onLowMemory() {
super.onLowMemory()
mapView.onLowMemory()
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:Android="http://schemas.Android.com/apk/res/Android"
xmlns:tools="http://schemas.Android.com/tools" package="com.murgupluoglu.googlemap">
<uses-permission Android:name="Android.permission.INTERNET"/>
<uses-permission Android:name="Android.permission.ACCESS_FINE_LOCATION" />
<application
Android:allowBackup="true"
Android:icon="@mipmap/ic_launcher"
Android:label="@string/app_name"
Android:roundIcon="@mipmap/ic_launcher_round"
Android:supportsRtl="true"
Android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<meta-data
Android:name="com.google.Android.geo.API_KEY"
Android:value="API_KEY_HERE" />
<activity Android:name=".MainActivity">
<intent-filter>
<action Android:name="Android.intent.action.MAIN"/>
<category Android:name="Android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:Android="http://schemas.Android.com/apk/res/Android"
xmlns:tools="http://schemas.Android.com/tools"
xmlns:app="http://schemas.Android.com/apk/res-auto"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.Android.gms.maps.MapView
Android:layout_width="0dp"
Android:layout_height="0dp"
Android:id="@+id/mapView"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>