I try some Code with a Map in a fragemt.
It works but the Map ID didnt found.
I get the warning that getMap is deprecated.
public class MapsActivity extends Fragment {
MapView mMapView;
private GoogleMap googleMap;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// inflat and return the layout
View v = inflater.inflate(R.layout.activity_maps, container,
false);
mMapView = (MapView) v.findViewById(R.id.mapView);
mMapView.onCreate(savedInstanceState);
mMapView.onResume();// needed to get the map to display immediately
try {
MapsInitializer.initialize(getActivity().getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}
googleMap = mMapView.getMap();
// latitude and longitude
double latitude = 17.385044;
double longitude = 78.486671;
The getMap() method in MapView and MapFragment is now deprecated in
favor of the new getMapAsync() method.
Google provided sample code for this:
public class MainActivity extends FragmentActivity
implements OnMapReadyCallback {
...
}
and when initializing fragment:
MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
when map is ready, you callback will be called
#Override
public void onMapReady(GoogleMap map) {
map.addMarker(new MarkerOptions()
.position(new LatLng(0, 0))
.title("Marker"));
}
Please one try to change the app:gradle because the getMap() helps some versions like
compile 'com.google.android.gms:play-services-maps:7.8.0'
compile 'com.google.android.gms:play-services-location:7.8.0'
then try to sync the code again!
Related
I have two Google Maps in my application, which I have implemented in the same way. One is working and one is not. The latter looks like an image and I can't interact with it. I can add markers and move the camera, but the info window doesn't show when I click the marker I can't move it, zoom in and zoom out. It looks like an image, a preview.
Here is the code:
public class MapFragment extends Fragment implements OnMapReadyCallback{
private GoogleMap mMap;
private static Double latitude;
private static Double longitude;
private static String title;
public static MapFragment newInstance(Double mLatitude, Double mLongitude, String mTitle) {
latitude=mLatitude;
longitude=mLongitude;
title=mTitle;
MapFragment fragment = new MapFragment();
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_map, container, false);
SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager()
.findFragmentById(R.id.map_details);
mapFragment.getMapAsync(this);
return view;
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.getUiSettings().setAllGesturesEnabled(true);
mMap.getUiSettings().setMapToolbarEnabled(false);
mMap.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromResource(R.drawable.location_icon))
.position(new LatLng(latitude, longitude))
.title(title));
CameraPosition cameraPosition = new CameraPosition.Builder().target(new LatLng(latitude,longitude)).zoom(12f).build();
CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition);
mMap.moveCamera(cameraUpdate);
}
public interface OnMapFragmentInteractionListener {
void onMapFragmentInteraction(Uri uri);
}
#Override
public void onResume() {
super.onResume();
}
#Override
public void onPause() {
super.onPause();
}
#Override
public void onDestroy() {
super.onDestroy();
}
#Override
public void onLowMemory() {
super.onLowMemory();
}
}
You should take a look at XML. There is liteMode and when it's set to true it does exactly what you are describing.
<com.google.android.gms.maps.MapView
android:id="#+id/row_map"
android:layout_width="match_parent"
android:layout_height="match_parent"
map:liteMode="true"
map:mapType="none" />
Here look at documentation
I'm just guessing but in code that you posted i see one mistake. To be sure you have to check your imports in the place when you try to use your MapFragment. As I said I guess that in one place it's imported from google pacage and another one from your class. The problem is that the MapFragment that you post here isn't a google MapFragment because you extants it from simple Fragment. To be sure rename your own MapFragment to CustomMapFragment and try to extands from MapFragment or just use it without customize.
{in.mpo.mpmobile/in.mpo.mpmobile.KioskListLocation}:
java.lang.ClassCastException: in.mpo.mpmobile.KioskListLocation cannot be cast to android.app.Activity
KioskListLocation class:
public class KioskListLocation extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.acctivity_kiosk_location, container, false);
mMapView = (MapView) rootView.findViewById(R.id.mapView);
mMapView.onCreate(savedInstanceState);
mMapView.onResume(); // needed to get the map to display immediately
try
{
MapsInitializer.initialize(getActivity().getApplicationContext());
}
catch (Exception e) {
e.printStackTrace();
}
mMapView.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(GoogleMap mMap) {
googleMap = mMap;
// For showing a move to my location button
googleMap.setMyLocationEnabled(true);
// For dropping a marker at a point on the Map
LatLng sydney = new LatLng(-34, 151);
googleMap.addMarker(new MarkerOptions().position(sydney).title("Marker Title").snippet("Marker Description"));
// For zooming automatically to the location of the marker
CameraPosition cameraPosition = new CameraPosition.Builder().target(sydney).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
});
return rootView;
}
}
Your code listing is missing the class declaration.
I guess you forgot to extend Activity.
You should reformat and expand your listing.
I have two errors in my code .. the following lines have the errors. Cannot resolve onCreate() method and getMapAsync(). I have done invalidate caches and restart but it didn't help. Please help me solve this issue.
mapView.onCreate(savedInstanceState);
map = mapView.getMapAsync();
public class PlacesMapActivity extends Fragment implements OnMapReadyCallback {
private MapView mapView;
private GoogleMap map;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.map_places, container, false);
// Gets the MapView from the XML layout and creates it
mapView = (MapView) v.findViewById(R.id.map);
mapView.onCreate(savedInstanceState);
// Gets to GoogleMap from the MapView and does initialization stuff
map = mapView.getMapAsync();
map.getUiSettings().setMyLocationButtonEnabled(false);
map.setMyLocationEnabled(true);
// Needs to call MapsInitializer before doing any CameraUpdateFactory calls
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);
return v;
}
#Override
public void onMapReady(GoogleMap googleMap) {
}
}
Do not return anything: map = mapView.getMapAsync();
use:
mapView.getMapAsync(this);
then inside onMapReady() get the Map object
#Override
public void onMapReady(GoogleMap googleMap) {
map= googleMap;
}
I tried to fetch a google map with current location but I got this error
FATAL EXCEPTION: main java.lang.NullPointerException
in
map= ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
java code
public class MapFragment extends Fragment{
private TextView locationText;
private TextView addressText;
private GoogleMap map;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.fragment_map, container, false);
locationText = (TextView) rootview.findViewById(R.id.location);
addressText = (TextView) rootview.findViewById(R.id.address);
//replace GOOGLE MAP fragment in this Activity
return rootview;
}
public void onMapReady(GoogleMap map) {
//make the call here. it will be called once the map is ready
replaceMapFragment();
}
private void replaceMapFragment() {
map= ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
// Enable Zoom
map.getUiSettings().setZoomGesturesEnabled(true);
//set Map TYPE
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
//enable Current location Button
map.setMyLocationEnabled(true);
//set "listener" for changing my location
map.setOnMyLocationChangeListener(myLocationChangeListener());
}
private GoogleMap.OnMyLocationChangeListener myLocationChangeListener() {
return new GoogleMap.OnMyLocationChangeListener() {
#Override
public void onMyLocationChange(Location location) {
LatLng loc = new LatLng(location.getLatitude(), location.getLongitude());
double longitude = location.getLongitude();
double latitude = location.getLatitude();
Marker marker;
marker = map.addMarker(new MarkerOptions().position(loc));
map.animateCamera(CameraUpdateFactory.newLatLngZoom(loc, 16.0f));
locationText.setText("You are at [" + longitude + " ; " + latitude + " ]");
//get current address by invoke an AsyncTask object
//new GetAddressTask(getActivity()).execute(String.valueOf(latitude), String.valueOf(longitude));
}
};
}
}
This may happened because the map is not ready at the moment.
#Override
public void onMapReady(GoogleMap googleMap) {
//make the call here. it will be called once the map is ready
replaceMapFragment();
}
In response to your comment on Kasun's answer, to display the current location marker on the map, call:
googleMap.setMyLocationEnabled(true);
Edit
import com.google.android.gms.maps.SupportMapFragment;
public class MapFragment extends SupportMapFragment
implements OnMapReadyCallback {
#Override
public void onCreate(Bundle inState) {
super.onCreate(inState);
// start a task for connecting to the map
// onMapReady() will be called when it is complete
getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
map = googleMap
// Enable Zoom
map.getUiSettings().setZoomGesturesEnabled(true);
//set Map TYPE
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
//enable Current location Button
map.setMyLocationEnabled(true);
//set "listener" for changing my location
map.setOnMyLocationChangeListener(myLocationChangeListener());
}
}
public class OutdoorFragment extends Fragment {
private GoogleMap googleMap;
double latitude = 1.31039;
double longitude = 103.7784;
public OutdoorFragment() {
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_outdoor, container,
false);
try {
// Loading map
initilizeMap();
} catch (Exception e) {
e.printStackTrace();
}
googleMap.getUiSettings().setZoomGesturesEnabled(true);
googleMap.getUiSettings().setRotateGesturesEnabled(true);
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
googleMap.setMyLocationEnabled(true);
// create marker
MarkerOptions marker = new MarkerOptions().position(
new LatLng(latitude, longitude)).title("Koh Chi Hao ");
// adding marker
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_VIOLET));
googleMap.addMarker(marker);
return rootView;
}
private void initilizeMap() {
if (googleMap == null) {
googleMap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getActivity().getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
}
#Override
public void onResume() {
super.onResume();
initilizeMap();
}
}
Hi there, inside my initializeMap() , googleMap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); keeps on saying that Cannot cast from Fragment to SupportMapFragment . I am not sure what is happening .
Thanks in advance
Change;
googleMap = ((SupportMapFragment) getFragmentManager().
findFragmentById(R.id.map)).getMap();
with this;
googleMap = ((SupportMapFragment) getSupportFragmentManager().
findFragmentById(R.id.map)).getMap();
Note that getSupportFragmentManager is from android.support.v4.app.FragmentActivity.
Good luck.
You need to use android.support.v7.app.Fragment;
And need to use getSupportFragmentManager
Basically you need v7 library
Check your activity layout. The fragment with map must have the following attribute name="com.google.android.gms.maps.SupportMapFragment" to indicate which fragment class you use.