i write simple google map v2 android application but in my java file
this line:
googleMap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
have error!
error:the getFragmentManager() is undefined for the type mainActivity.
please help me my friends.
If you used SupportMapFragment then used getSupportFragmentManager()
and if you used MapFragment then used getFragmentManager()
Correct
googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
Try This code:
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
Related
I have recently started working on an android application that includes the GPS tracking of the user's position.
For that purpose I have gone through Google's guide to setting up the GoogleMaps API for Android and I have (to my knowledge) fully understood the steps it took me to get here including the correct API_KEY and the SHA1 Footprint for the debug version of google maps.
The included code sends a request for the map information to google's API and upon loading fully sets up the map and places a marker in Sydney.
But it doesn't place the marker. Or move the maps interface to Sydney. It just shows the map and appears to be centering itself around africas west-coast every time for some reason.
Here's how far i've gotten in the Java Activity code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pkw__route__content__gps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
try {
mapFragment.getMapAsync(this);
}
catch(NullPointerException npe){
npe.printStackTrace();
}
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney, Australia, and move the camera.
LatLng sydney = new LatLng(-34, 151);
current = mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
The fragment looks as follows:
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="257dp"
android:layout_height="224dp"
android:layout_marginStart="72dp"
android:layout_marginTop="15dp"
android:layout_marginEnd="72dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:context=".MapsActivity" />
I have added the API key to the Manifest in a tag called "com.google.android.geo.API_KEY". I will not post it in here for obvious reasons.
As said, the Map initializes correctly but the Markers are not placed and the moveCamera call is also not executed correctly
EDIT:
I think this question needs some more backstory, I am using the Map inside a fragment which is integrated into another activity.
The reason why I think this is important is because I have tried to start the map activity through an intent in which case it will fully load up, place markers and work interactively as it should. Because all of this is in the onCreate() method. How do I do this without having to invoke the Activity and stop the other one?
The map is not loaded when onMapReady(GoogleMap googleMap) is called. You have to wait for the OnMapLoadedCallback Event.
You have set a OnMapLoadedCallback to the map in onMapReady(GoogleMap googleMap) and set the marker and zoom the map to the location in this callback.
I am using SupportMapFragment to display Google Maps in my app:
SupportMapFragment mMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentByTag(MAP_FRAGMENT_TAG);
if (mMapFragment == null) {
mMapFragment = new SupportMapFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.map_frame, mMapFragment, MAP_FRAGMENT_TAG).commit();
}
mMapFragment.getMapAsync(this);
This is the xml part:
<FrameLayout
android:id="#+id/map_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/button_status_view">
</FrameLayout>
Unfortunately, the map doesn't show the Google logo which causes violation on the Google Maps APIs terms of service (https://developers.google.com/maps/terms) In particular clause 9.4(a), which does not allow developers to delete or alter the Google logo on the map displayed in your app.
How can I add the Google logo to the map? Should it be there by default? Am I missing something? The fragment currently shows only the GPS button.
use MapFragment instead of Frame Layout:
<fragment android:id="#+id/map" android:name="com.google.android.gms.maps.SupportMapFragment" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.googlemapdemo.MapsActivity"/>
I am using a mapview in listview,it's a part of item view in the listview,now i need to change the api v1 to v2,but i can not,because the new api only support the GoogleMap view with a frgment,i don't know how to implements that,it always say "java.lang.ClassCastException: com.google.android.gms.maps.MapFragment cannot be cast to android.support.v4.app.Fragment".It's anyone here can help?Thank you very much!
the key code what i use is same with blew:
in layout,i add the follow code to a LinearLayout:
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
and in the java code,i try to get the GoogleMap view in this way(in the getView method of my own adapter with extends the BaseAdapter),here is the code:
GoogleMap mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
The error you recieve derived from the fact that you are trying to get a MapFragment object as it's specified in your XML file using the SupportFragmentManager object.
Instead use the FragmentMananger object to get the MapFragment, like so:
GoogleMap mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
Of course this depends on what is you target API you develop you application to. if your taget api > 11 then use the following line and a simple Activity class. in other case change you XML object of the map to SupportMapFragment and use the line you have used:
GoogleMap mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
in this case you Acitivity should extend an FragmentActivity object.
try using like this
`
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
`
and java code like this
GoogleMap mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
and make sure your class extends FragmentActivity
I have added a map fragment to my app including a geographic marker and included my play services jar but I'm getting the following error on my marker code:
Error: Cannot cast from Fragment to SupportMapFragment
The snippet is as follows:
GoogleMap mMap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
mMap.addMarker(new MarkerOptions()
.position(new LatLng(53.271900177, -9.04889965057))
.title("GMI alway Location"));;
This is my xml declaration to reference it:
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true" />
Anyone have any ideas as to where I'm going wrong? Thanks Brian J
You have to use the SupportFragmentManager and the FragmentActivity classes if you are using the SupportMapFragment in your layout.
So use the getSupportFragmentManager() method instead of getFragmentManager().
friends, I am getting NullPointer error ,on addMarker()
Below is my sample code.
GoogleMap myMap;
android.support.v4.app.FragmentManager myFragmentManager = getSupportFragmentManager();
SupportMapFragment myMapFragment = (SupportMapFragment)myFragmentManager.findFragmentById(R.id.map);
myMap = myMapFragment.getMap();
myMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
myMap.setMyLocationEnabled(true);
myMap.getUiSettings().setZoomControlsEnabled(true);
myMap.getUiSettings().setCompassEnabled(true);
myMap.getUiSettings().setMyLocationButtonEnabled(true);
Error message like NullPointer at line
myMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".DemoMapActiviy" >
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
</LinearLayout>
any idea how can i solve it?
I went through this issue a while back. First check to see if your fragment is found by the fragment manager.
If not try replacing the way you've put your fragment into your view. If you want an example of another way I've posted a maps example here
Any more questions feel free to comment and I'll work through it with you :)
EDIT
have you got the correct API key in there? If you can't contact Google's servers then there's a good chance that that's your problem. Otherwise may be a manifest issue make sure you have READ_GSERVICES and INTERNET permissions.
See Google Maps API V2 'Failed to Load Map. Could not contact Google Servers'
Edit 2
Ok so I was being lazy before and just referring you to my project. If you were getting a connection to maps in your project lets start from there.
What I was suggesting in my original response was to check and see if myMapFragment was null. If it is great try doing something along the lines of the following:
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
SupportMapFragment myMapFragment = new SupportMapFragment();
ft.replace(R.id.map, myMapFragment, Constant.TAXI_FRAGMENT_ID);
ft.commit();
Where R.id.map is an empty FrameLayout
If myMapFragment isn't null then try extending SupportMapFragment and running the following test:
public class MyMapFragment extends SupportMapFragment {
#Override
public void onActivityCreated()
{
if (getMap() == null) Log.e("TAG", "There's something very wrong here";
}
}
Also if you do this make sure that you are inflating an instance of MyMapFragment not of SupportMapFragment