How do I load google map in async task. My app requires that when a user clicks a dialog box, it loads a google map activity. However it takes a lot of time for the app to load the map activity. I would like the loading of the map to be a bit faster. or possibly notify the user that google map is loading. I feel async task will be the best way to do this. But how do i achieve this through async task? Or is there a better of doing this? thank you.
I hope this helps.
make your activity implement OnMapReadyCallback
mapFragment = (MapFragment) getFragmentManager().findFragmentById(
R.id.map);
//start a progress dialog here
mapFragment.getMapAsync(this);
#Override
public void onMapReady(GoogleMap arg0) {
mMap = arg0;
//cancel the progress dialog here
}
Related
I have opened "Google Map" Application from my App.
What I want is I want to add my app button for navigating to my app, over only google map not on other apps.
Using this link ,I have added overlayservice to my app but it will overdraw over every other application on device window.
I only want to draw over google map app Like this.
This started off as a comment, and then morphed into something of an answer. One option would be to draw a custom marker on your Google Map for the taxi icon. Whenever there is a zoom, drag, etc., you might have to do a re-render to get it in the same place again. I can verify that it is possible to capture click events on a custom marker in Android. Here is what your setup might look like:
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setOnMarkerClickListener(this);
mMap.setOnCameraMoveListener(this);
// ...
}
Then, override the marker click listener:
#Override
public void onCameraMove() {
// remove the old marker, and redraw it again on the left of the screen
}
You also probably would want to capture click events on your custom marker:
#Override
public boolean onMarkerClick(final Marker marker) {
// ...
}
The above onMarkerClick() method receives a reference to the marker which was clicked. You may keep an activity-scoped marker, and then check if it matches what was clicked to decide whether or not your custom marker were clicked.
I am struggling with animateCamera() method of the Google Maps. It only works on first launch of activity and if the activity is destroyed and created again, camera animations doesn't work but map is loaded fine. I have tried debugging the code, everything gets executed but map doesn't animate without any error or log. Although animation works when same mapFragment is used in the fragment but in activity it doesn't seem to work.
The solution mentioned in this this question is deprecated now and I am unable to fix this issue.
Map Fragment
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
mapFragment.setRetainInstance(true);
#Override
public void onMapReady(GoogleMap googleMap) {
if (mMap == null) mMap = googleMap;
}
Code for animating camera
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, ZOOM_LEVEL));
Making entire project again from old code fixed the issue. But this issue re-appeared in another app. I found out that application was using old reference to mMap. This can be fixed by either getting reference from fragment manager or just setting mMap = null when activity is destroyed.
As far as I can see, MapFragment has an issue with transition animations. All views on the layout are getting shown immediately, including the MapFragment's own views (like zoom buttons). But the map itself gets loaded with a lag only after the animation is completed.
In order to illustrate the problem, I did the following:
I changed one of the Activities in the Google maps android API examples slightly. It opens a blank activity via an Action Item. When I click back button, the map gets loaded, but only after the transition is completed.
I exaggerated the transition effect a little bit, so that you can see the problem better. I set the transition animation speed in Developer Options to 5x. Even on 1x speed, this lag is disturbing though.
See this video: http://www.youtube.com/watch?v=12SEotktlXI
Do you have any suggestion to prevent this lag? Why do all views get loaded immediately but the map itself doesn't?
Testing environment: Nexus 5, Android 4.4.2, unrooted
Edit: This problem also occures when MapView is used instead of MapFragment.
Reason: Its because as soon as you settings activity will be shown, the map activity will be on its onpause() state; thus, I assume android management reclaimed the memory from the map activity.
Solution:
Make a static class and declare you map statically there, to avoid android from reclaiming the memory used by your map.
Ex.
//your static class
public class MapData{
public static GoogleMap map;
}
//your map activity
public class MapActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(MapData.map != null)
MapData.map = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
}
}
I have an map fragment in my android application. What I want to do is make it clickable so that if someone clicks anywhere on the fragment I can start a new activity. I tried doing some research but all I'm coming up with is how to make map markers clickable or not clickable. My map fragment only takes up a small portion of my activity and shows only one map marker.
Try this:
map.setOnMapClickListener(new OnMapClickListener() {
#Override
public void onMapClick(LatLng point) {
startActivity(new Intent(yourActivity, nextActivity.class));
}
});
i think you do it implementing on touch listener on map fragment view it will work for me, if click on map fragment u can open map full view in another activity.
may be this will be help u
When the application opens I create a new instance of a google map during onAttachedToWindow(), this works fine while the application is open. When I close the application and re-open it the google map does not load correctly, it just shows grey tiles.
#Override
public void onAttachedToWindow() {
if (hasCreated()) {
uiGoogleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map_content_map_fragment)).getMap();
}
}
When should the google map be initialised to ensure it is loaded when the application is closed and re-opened?
EDIT:
I have tried initialising the map in onResume() and onCreate() and get the same problem
on Google API2 this hassle has been eliminated.
What you need to do is just initialize the map you created on your xml.
uiGoogleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map_content_map_fragment))
.getMap();
So when you close and re-open it it will be available. no need to override anything.
I think that the best place would be to create a class that extends SupportMapFragment, and initialise the map in:
#Override
public void onAttach(Activity activity)
Besides that though, I think if you initialise the map in onResume of the container activity it should be ok. But if you by initialising you mean adding the mapfragment to the activity, I would suggest doing it in the xml or in your activity's onCreate. Just my two pennies.