google maps - android: 'inconvertible types' error on googlemap setup -
there tutorial setup google map on android here: https://gist.github.com/joshdholtz/4522551
i can run in application.
but google replace getmapasync()
getmap()
here new google tutorial: https://developers.google.com/maps/documentation/android-api/start
i try convert fragment:
public class mfragment extends fragment implements onmapreadycallback { mapview gmapview; googlemap gmap = null; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.m_layout, container, false); gmapview = (mapview) view.findviewbyid(r.id.map); gmapview.getmapasync(this); return view; } @override public void onmapready(googlemap map) { gmap = map; gmap.setmaptype(googlemap.map_type_hybrid); gmap.movecamera(cameraupdatefactory.newlatlngzoom(new latlng(49.39,-124.83), 20)); } }
m_layout.xml:
<fragment 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:id="@+id/map" tools:context="mfragment" android:name="com.google.android.gms.maps.supportmapfragment" />
but in debug level got error:
error:(24, 75) error: inconvertible types required: mapfragment found: fragment
on line:
gmapfragment = (mapfragment) getfragmentmanager().findfragmentbyid(r.id.map);
what's wrong?
a mapview
not supportmapfragment
.
you have 2 options, either use nested supportmapfragment, or make fragment extend supportmapfragment.
for first option, key use getchildfragmentmanager()
in order supportmapfragment:
public class mfragment extends fragment implements onmapreadycallback { googlemap gmap; supportmapfragment mapfrag; public mfragment () { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.m_layout, container, false); mapfrag = ((supportmapfragment) getchildfragmentmanager().findfragmentbyid(r.id.map)); mapfrag.getmapasync(this); return rootview; } @override public void onmapready(googlemap map) { gmap = map; gmap.setmaptype(googlemap.map_type_hybrid); gmap.movecamera(cameraupdatefactory.newlatlngzoom(new latlng(49.39,-124.83), 20)); } }
with second option, fragment extends supportmapfragment, , there no need inflate layout xml:
public class mfragment extends supportmapfragment implements onmapreadycallback { googlemap gmap; public mfragment() { } @override public void onresume() { super.onresume(); setupmapifneeded(); } private void setupmapifneeded() { if (gmap == null) { getmapasync(this); } } @override public void onmapready(googlemap map) { gmap = map; gmap.setmaptype(googlemap.map_type_hybrid); gmap.movecamera(cameraupdatefactory.newlatlngzoom(new latlng(49.39,-124.83), 20)); } }
for more details might in next steps, take @ this answer.
Comments
Post a Comment