Reputation: 390
I need to add a SupportMapFragment created with AndroidStudio. The fragment activity that uses it works fine. It is showing a map with is marker on Sidney. But I need to put that map on a Layout (i.e., a LinearLayout) to add buttons and interact with the map.
I create a class MapsActivity.java with its corresponding layout activity_maps.xml
<?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">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Some Text"
android:id="@+id/textView" />
<fragment xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scrollbars="vertical"
class="com.google.android.gms.maps.SupportMapFragment"/>
</LinearLayout>
Being that: public class MapsActivity extends AppCompatActivity, Inside the MainActivity.onCreate() I tried:
1)
android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
SupportMapFragment mapFragment = SupportMapFragment.newInstance();
manager.beginTransaction().replace(R.id.map, mapFragment).commit();
setContentView(R.layout.activity_maps);
GoogleMap mMap = mapFragment.getMap();
// NOT GOOD: nMap is null.
2)
FragmentManager manager = getChildFragmentManager();
[...]
// NOT GOOD: Cannot resolve method getChildFragmentManager.
3)
GoogleMap mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
// NOT GOOD: nMap is null.
What can I try next?
Upvotes: 2
Views: 12466
Reputation: 761
This way to show google map in your screen
<fragment
android:id="@+id/map"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
public class RouteFinderActivity extends AppCompatActivity implements OnMapReadyCallback {
GoogleMap map;
Location location;
Button btn_search_route;
EditText et_source, et_destination;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_route_finder);
btn_search_route = findViewById(R.id.btn_search_route);
et_source = findViewById(R.id.et_source);
et_destination = findViewById(R.id.et_destination);
btn_search_route.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String source = et_source.getText().toString();
String destination = et_destination.getText().toString();
if (TextUtils.isEmpty(source)) {
et_source.setError("Enter Soruce point");
} else if (TextUtils.isEmpty(destination)) {
et_destination.setError("Enter Destination Point");
} else {
String sendstring="http://maps.google.com/maps?saddr=" +
source +
"&daddr=" +
destination;
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse(sendstring));
startActivity(intent);
}
}
});
((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
map = googleMap;
map.setMapType(1);
map.setMyLocationEnabled(true);
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
&& ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false));
if (location != null) {
//zoom
CameraPosition cameraPosition = new CameraPosition.Builder()
.zoom(17)
.target(new LatLng(location.getLatitude(), location.getLongitude()))
.bearing(90)
.tilt(40)
.build();
map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
}
}
}
Manifest
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="apikeymap" />
Upvotes: 0
Reputation: 390
I publish here the complete answer for who will be interested, I found it not so intuitive. This lets you add a map to your custom Layout.
public class MapsActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
FragmentManager myFragmentManager = getSupportFragmentManager();
SupportMapFragment mapFragment = (SupportMapFragment) myFragmentManager.findFragmentById(R.id.map);
GoogleMap mMap = mapFragment.getMap();
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-35, 152);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker near Sidney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
The layout:
<?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">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Some Text"
android:id="@+id/textView" />
<fragment xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scrollbars="vertical"
class="com.google.android.gms.maps.SupportMapFragment"/>
</LinearLayout>
Upvotes: 0
Reputation: 157467
let your Activity
implement OnMapReadyCallback
. E.g.
public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback {
in onCreate
, after setContentView
, you do
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
When onMapReady
is invoked you can use googleMap
@Override
public void onMapReady(GoogleMap googleMap) {
It can take a while before the GoogleMap
object is ready for you to use, hence getMapAsync
and the OnMapReadyCallback
Upvotes: 1
Reputation: 6834
Try like this. Modify First one
You are creating new
MapFragment
usingSupportMapFragment.newInstance();
rather getting from xml mapFragment
FragmentManager myFragmentManager = getSupportFragmentManager();
mapFragment = (SupportMapFragment) myFragmentManager
.findFragmentById(R.id.map);
map = mapFragment.getMap();
Upvotes: 2