Next.
Modify MapsActivity:
MapsActivity Activity
Implement four interfaces:
Android. location. LocationListener
GoogleMap. InfoWindowAdapter
GoogleMap. OnMarkerClickListener
GoogleMap. OnMarkerDragListener
This article mainly uses the LocationListener interface to implement location on a map.
The other three interfaces are related to the marking (Marker), moving the marking points, and clicking the marking to bring up the info window. These functions will be sorted in the next article.
Map Initialization
First, you need to set the map object in onCreate:
ServicesConnected: Check whether the service is available
resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable( (ConnectionResult.SUCCESS =="Google Play services is available."= ConnectionResult connectionResult = ConnectionResult(resultCode, errorCode = Dialog errorDialog = (errorDialog != =
As mentioned in the previous article, Google Play and play store must be installed in the mobile phone debugging environment. If it is not installed, the error code is returned here.
InitMapView Initialization
mMapView = UiSettings setting = setting.setTiltGesturesEnabled(
Two rows to obtain the GoogleMap mMapView. Many subsequent operations must be performed on the map object.
3 rows, set to normal in map Mode
In line 4, UiSettings sets various button gestures related to human-computer interaction, such:
Void setTiltGesturesEnabled (boolean) whether to allow a gesture to change the angle of view;
Void setCompassEnabled (boolean) Whether to display the compass;
For detailed UiSettings usage, refer to official https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/UiSettings
Move to longitude and latitude location
First, let's clarify a concept. Goolge Map assumes that the Map itself is not fixed and it moves camera (public final class CameraUpdate ).
Imagine that a canon was floating over the earth, aiming at the Magic capital. The focal length was narrowed closer to line 1, and the angle of view was tilted to see the full view of the magic capital, or with wide angle. Good!
Go back to the code and use GPS here. Use LocationManager to obtain the location service
mLocManager == mLocManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
Obtain the LocationManager and check whether the GPS is available.
Register the listener in the onResume Function
(isServiceOk == String provider = (provider != mLocManager.requestLocationUpdates(provider, 5*1000, 1, }
Line 7: Obtain the available Location Provider. If GPS is enabled, the GPS provider is obtained.
Line 9: register the location change listener. The second input parameter 5*1000 indicates updating every 5 seconds, and the third input parameter indicates moving more than 1 meter. The last input parameter is LocationListener. Because activity implement has LocationListener, you only need to give this pointer to activity.
Two Functions of row 12 and row 13 are used to actively obtain the latest location, move the map to this location, and paste it later.
First, let's take a look at the location change listening function. After implement LocationListener, the activity needs to implement several functions:
(mLocation != onStatusChanged(String arg0,
3 ~ Nine rows. I only process them here.
Location mLocation =
Function, save the new location to mLocation, and then call to move the map to this location.
Sometimes we need to take the initiative to query the latest Location
String bestProvider = Location newLoction = (bestProvider != newLoction = (mLocation == mLocation = Location("" (newLoction != }
3 rows to obtain the optimal provider
Seven rows to obtain the last location
8 ~ 16 rows. Similarly, the new location is recorded in the mLocation.
The getBestProvider function is as follows:
Private String getBestProvider (){
Criteria criteria = new Criteria ();
Criteria. setPowerRequirement (Criteria. POWER_LOW );
Criteria. setAccuracy (Criteria. ACCURACY_FINE );
String bestOne = mLocManager. getBestProvider (criteria, true );
Return bestOne;
}
The two functions used above
setLatLng( (mLocation == Toast.makeText( dLat = dLong = log("setLatLng: (" + dLat + "," + dLong + ")" LatLng latlng = ((latlng.latitude == 0) && (latlng.longitude == 0 } mMapView.moveCamera(CameraUpdateFactory.newLatLngZoom(latlng, 15 animateLatLng( (mLocation == Toast.makeText( dLat = dLong = log("animateLatLng: (" + dLat + "," + dLong + ")" LatLng latlng = }
First look at the first one:
7 ~ Line 8: Call getLatitude () in mLocation to obtain the dimension, and getlongdistance () to obtain the longitude.
12 rows to construct a LatLng object
Line 16, mMapView. moveCamera (CameraUpdateFactory. newLatLngZoom (latlng, 15 ));
CameraUpdateFactory. newLatLngZoom (latlng, 15) returns a CameraUpdate object. The input parameters are longitude and latitude and zoom level;
The moveCamera method of GoogleMap moves the map to this position.
Function
Lines 31 are basically the same. The only difference is that the last call is animateCamera. We will see the process of moving the map from the original location to the new location. The moveCamera method is instantaneous and does not see the moving process.
There are many constructor methods in CameraUpdate. You can specify the position and the magnification individually or simultaneously. Specify the boundary wait. For details, see
Https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/CameraUpdateFactory
Finally, log out of the location service listener in the onPause function.
MLocManager. removeUpdates (this );