Android: How to mark on Google Map in custom dialog? - android

what I am trying is: to show the google map with starting and end point marks in alert dialog on external activity.
So I did:
1) Create a layout, my_dialog_view.XML for a dialog:(NOTE: this layout is apart from the main layout for the activity where a dialog is called)
<RelativeLayout 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"
tools:context="com.example.exampleApp">
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
2) Define a view for the dialog layout:
LayoutInflater factory = LayoutInflater.from(this);
v = factory.inflate(R.layout.my_dialog_view, null);
3) Define Google map handler:
GoogleMap gmap = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
4) Set markers of starting and end point:
LatLng startLoc = new LatLng((double)lat,(double)lng);
CameraPosition camPos = new CameraPosition.Builder().target(startLoc)
.zoom(15).bearing(45).tilt(70).build();
CameraUpdate camUpd3 = CameraUpdateFactory.newCameraPosition(camPos);
gmap.animateCamera(camUpd3);
MarkerOptions markerOpts = new MarkerOptions().position(startLoc).title(
"Start Location");
gmap.addMarker(markerOpts);
And I have an error as follows:
Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.GoogleMap com.google.android.gms.maps.SupportMapFragment.getMap()' on a null object reference
It makes sense to have this error since the layout set for the activity (where the java code are written) does not have the fragment component that I tried to use in 3) step.
Then my question is: How to properly define a google map handler with the external dialog layout (by using LayoutInflater or any other possible solution) to resolve the null reference error?
Any input will be GREATLY appreciated.
Best,

Related

GoogleMap complains of being a null object reference

I am working on an android application where I use maps and this is the error that I am getting
Attempt to invoke virtual method 'void com.google.android.gms.maps.GoogleMap.setMapType(int)' on a null object reference
Prior to posting my query here I have looked for other similar issues but my code already have all what is being suggested so I was hoping if someone could point out the error.
This is the line which throws the error(second line).
I have no idea why it is caling map null when I've assigned it (at least I think I have)
private void initViews() {
map = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMap();
//map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
and also this is my xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingTop="65dp" >
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
Use getMapAsync(...) instead of getMap().
Example can be found here.

Setting GoogleMapOptions programmatically

I am inflating my fragment like this:
GoogleMap map = ((MapFragment) getFragmentManager().findFragmentById(R.id.MapFragment_map_Fragment)).getMap();
and here I have my options:
GoogleMapOptions options = new GoogleMapOptions();
options.mapType(GoogleMap.MAP_TYPE_SATELLITE);
In the documentation I see that I need to use this:
To apply these options when you are creating a map, do one of the
following:
If you are using a MapFragment, use the
MapFragment.newInstance(GoogleMapOptions options) static factory
method to construct the fragment and pass in your custom configured
options.
But I don't understand how am I suppose to use this.
I think you can use GoogleMapOptions only if you are creating map view programmatically(passing options to MapFragment.newInstance() method - docs). You are inflating MapFragment from xml so you wont be able to use them in that way. In your case you can still change map options by using GoogleMap setters or UiSettings.
For example:
GoogleMap googleMap = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map_fragment)).getMap();
googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
I use LiteMode programmatically instead of setting attribute of MapView and MapFragment in XML file. Try this:
GoogleMapOptions googleMapOptions = new GoogleMapOptions().liteMode(true);
googleMap.setMapType(googleMapOptions.getMapType());
Or in XML:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
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.mapslastapp.MapsActivity"
map:liteMode="true"
map:cameraZoom="16"
map:mapType="normal"/>

How to add a new google map view(the new with api v2 for android) as a item into a listview?

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

Using geo location Google maps api v2

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().

GoogleMap class object throw Nullpointer Exception

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

Categories

Resources