-
-
Save joshdholtz/4522551 to your computer and use it in GitHub Desktop.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="fill_parent" | |
android:layout_height="fill_parent" > | |
<com.google.android.gms.maps.MapView android:id="@+id/mapview" | |
android:layout_width="fill_parent" | |
android:layout_height="fill_parent" /> | |
</LinearLayout> |
public class SomeFragment extends Fragment { | |
MapView mapView; | |
GoogleMap map; | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | |
View v = inflater.inflate(R.layout.some_layout, container, false); | |
// Gets the MapView from the XML layout and creates it | |
mapView = (MapView) v.findViewById(R.id.mapview); | |
mapView.onCreate(savedInstanceState); | |
// Gets to GoogleMap from the MapView and does initialization stuff | |
map = mapView.getMap(); | |
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 onResume() { | |
mapView.onResume(); | |
super.onResume(); | |
} | |
@Override | |
public void onDestroy() { | |
super.onDestroy(); | |
mapView.onDestroy(); | |
} | |
@Override | |
public void onLowMemory() { | |
super.onLowMemory(); | |
mapView.onLowMemory(); | |
} | |
} |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="com.example" | |
android:versionCode="1" | |
android:versionName="1.0" > | |
<uses-sdk | |
android:minSdkVersion="8" | |
android:targetSdkVersion="15" /> | |
<uses-permission android:name="android.permission.INTERNET"/> | |
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/> | |
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> | |
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> | |
<uses-feature | |
android:glEsVersion="0x00020000" | |
android:required="true"/> | |
<permission | |
android:name="com.example.permission.MAPS_RECEIVE" | |
android:protectionLevel="signature"/> | |
<uses-permission android:name="com.example.permission.MAPS_RECEIVE"/> | |
<application | |
android:icon="@drawable/ic_launcher" | |
android:label="@string/app_name" | |
android:theme="@style/AppTheme" > | |
<meta-data | |
android:name="com.google.android.maps.v2.API_KEY" | |
android:value="your_key"/> | |
<activity | |
android:name=".HomeActivity" | |
android:label="@string/app_name" > | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
</application> | |
</manifest> |
Thanks dude... its very useful and working perfectly....
This gist saved me so much frustration! Thank you!
Great ...thanks a lot for this help
Greate ... thank you for this help.
If someone want to use this code not inside fragment - you should add mapView.onResume(), or you will have no tiles before touch on map.
very nice..... Thank you
Nice example ;)
Thanks.
Note that getMap() is now deprecated, use getMapAsync(this)
@igofed thanks for the point
Thanks!
Can I call super before the map?
public void onResume() {
super.onResume();
mapView.onResume();
}
This is great. Please how can I add sliding menu to this map ? want it to be a page of the side menu.
Awesome, thanks! :DD
My hero! XD
Thank you so much, you have made my learnng a lot easier ...bless
I have to touch at screen to get the map updated. Would you help me please with this issue ... thx
onResume did the trick.... :(
@Override
public void onResume() {
mapView.onResume();
super.onResume();
}
Thanks mate ;)
Thanks a lot :)
Thnx Dude , you made my day ^^
Used the exact code inside Dialog. But not showing the map
I wasted a whole day, just because I didn't add onResume() :( This is crazy, but thnx to the owner of this gist, may Allah bless you.
Getting inflating error
android.view.InflateException: Binary XML file line #37: Binary XML file line #37: Error inflating class com.google.android.gms.maps.MapView
Have you tried for Marshmallow? I am getting error EGL BAD DISPLAY.
Thanks man. Instead of searching the location by lat long I want to search it by Name of place or institutions,How can I do it?Please help
Thankyou its very usefull...
How have you used this fragment? My MainActivity extends FlutterActivity so how should I proceed?
When adding MapView to my XML I get the following error:
could not find dependency "com.google.android.gms:play-services-maps:11.8.0"
What to do?
Thank you very much for this code.
Further code about MapView
can be found at Google Maps Android Samples RawMapViewDemoActivity sample, and Maps SDK for Android Map Objects MapView guide.
After combining this gist code with the above mentioned links, I found it to work.
Thank you for this code.
How can I find current location with mapview?
very useful thank you
how to get api key from google
thanks! very useful