본문
180911(화) - Google Maps (Location data)
Mobile/Google Maps 2018. 9. 11. 17:36
Google Maps
Location data
- My Location layer
・ map 에 device's location을 표시하는 simple way를 제공
・ not provide data
- Google Play services Location API
・ recommended all programmatic request for location data
・ provide custom location provider
Location permissions
- ACCESS_COARSE_LOCATION
and ACCESS_FINE_LOCATION
-android.permission.ACCESS_COARSE_LOCATION
・ use WiFi or mobile cell data (or both)가 device's location 을 결정
・ 대략 equivalent city block으로 return
- android.permission.ACCESS_FINE_LOCATION
・ location provider로부터 precise location as possible
・ GPS + WiFi + cell data
Add the permissions to manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp" >
...
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
...
</manifest>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp" >
...
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
...
</manifest>
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
mMap.setMyLocationEnabled(true);
} else {
// Show rationale and request permission.
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == MY_LOCATION_REQUEST_CODE) {
if (permissions.length == 1 &&
permissions[0] == Manifest.permission.ACCESS_FINE_LOCATION &&
grantResults[0] == PackageManager.PERMISSION_GRANTED) {
mMap.setMyLocationEnabled(true);
} else {
// Permission was denied. Display an error message.
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == MY_LOCATION_REQUEST_CODE) {
if (permissions.length == 1 &&
permissions[0] == Manifest.permission.ACCESS_FINE_LOCATION &&
grantResults[0] == PackageManager.PERMISSION_GRANTED) {
mMap.setMyLocationEnabled(true);
} else {
// Permission was denied. Display an error message.
}
}
The My Location layer
ublic class MyLocationDemoActivity extends FragmentActivity
implements OnMyLocationButtonClickListener,
OnMyLocationClickListener,
OnMapReadyCallback {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_location_demo);
SupportMapFragment mapFragment =
(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap map) {
mMap = map;
// TODO: Before enabling the My Location layer, you must request
// location permission from the user. This sample does not include
// a request for location permission.
mMap.setMyLocationEnabled(true);
mMap.setOnMyLocationButtonClickListener(this);
mMap.setOnMyLocationClickListener(this);
}
@Override
public void onMyLocationClick(@NonNull Location location) {
Toast.makeText(this, "Current location:\n" + location, Toast.LENGTH_LONG).show();
}
@Override
public boolean onMyLocationButtonClick() {
Toast.makeText(this, "MyLocation button clicked", Toast.LENGTH_SHORT).show();
// Return false so that we don't consume the event and the default behavior still occurs
// (the camera animates to the user's current position).
return false;
}
}
Google Play Services Location API
'Mobile > Google Maps' 카테고리의 다른 글
180912(수) - Google Maps (Info Windows) (0) | 2018.09.12 |
---|---|
180911(화) - Google Maps (Markers) (0) | 2018.09.11 |
180910(월) - Google Maps (Camera and View) (0) | 2018.09.10 |
180910(월) - Google Maps (Events) (0) | 2018.09.10 |
180910(월) - Google Maps (Controls and Gestures) (0) | 2018.09.10 |
댓글