-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Yang #464
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Yang #464
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
72244e3
just for test
butterflyforever 7ac54d7
basic view and Running fragment
butterflyforever 8695acd
mapview
butterflyforever 34099ec
1. Using view model to store the status of fragment.
butterflyforever 54ce5cc
1. Use Data Binding to change Layout
butterflyforever 0bfd15c
Fix code reviews
butterflyforever File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
android/canonical/app/src/debug/res/values/google_maps_api.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<resources> | ||
<!-- | ||
TODO: Before you run your application, you need a Google Maps API key. | ||
|
||
To get one, follow this link, follow the directions and press "Create" at the end: | ||
|
||
https://console.developers.google.com/flows/enableapi?apiid=maps_android_backend&keyType=CLIENT_SIDE_ANDROID&r=CF:9E:07:3A:08:E9:41:50:B3:2D:4C:6E:50:04:A5:E2:6A:64:72:87%3Bcom.google.samples.quickstart.canonical | ||
|
||
You can also add your credentials to an existing key, using these values: | ||
|
||
Package name: | ||
com.google.samples.quickstart.canonical | ||
|
||
SHA-1 certificate fingerprint: | ||
CF:9E:07:3A:08:E9:41:50:B3:2D:4C:6E:50:04:A5:E2:6A:64:72:87 | ||
|
||
Alternatively, follow the directions here: | ||
https://developers.google.com/maps/documentation/android/start#get-key | ||
|
||
Once you have your key (it starts with "AIza"), replace the "google_maps_key" | ||
string in this file. | ||
--> | ||
<string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">AIzaSyDz7brZFFw7tIg8bQLCALELeHp4JNkZmJo</string> | ||
|
||
</resources> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
android/canonical/app/src/main/java/com/google/samples/quickstart/canonical/MapsFragment.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package com.google.samples.quickstart.canonical | ||
|
||
import android.Manifest | ||
import android.app.Activity | ||
import android.content.pm.PackageManager | ||
import android.location.Location | ||
import androidx.fragment.app.Fragment | ||
|
||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import androidx.core.content.ContextCompat.checkSelfPermission | ||
import com.google.android.gms.location.FusedLocationProviderClient | ||
import com.google.android.gms.location.LocationServices | ||
|
||
import com.google.android.gms.maps.CameraUpdateFactory | ||
import com.google.android.gms.maps.GoogleMap | ||
import com.google.android.gms.maps.OnMapReadyCallback | ||
import com.google.android.gms.maps.SupportMapFragment | ||
import com.google.android.gms.maps.model.LatLng | ||
import com.google.android.gms.maps.model.MarkerOptions | ||
|
||
class MapsFragment : Fragment() { | ||
|
||
private lateinit var map: GoogleMap | ||
private lateinit var fusedLocationClient: FusedLocationProviderClient | ||
private lateinit var lastLocation: Location | ||
|
||
companion object { | ||
private const val LOCATION_PERMISSION_REQUEST_CODE = 1 | ||
} | ||
|
||
override fun onRequestPermissionsResult(requestCode: Int, | ||
permissions: Array<String>, grantResults: IntArray) { | ||
when (requestCode) { | ||
LOCATION_PERMISSION_REQUEST_CODE -> { | ||
// If request is cancelled, the result arrays are empty. | ||
if ((grantResults.isNotEmpty() && | ||
grantResults[0] == PackageManager.PERMISSION_GRANTED)) { | ||
setUpMap() | ||
} else { | ||
// Explain to the user that the feature is unavailable because | ||
// the features requires a permission that the user has denied. | ||
} | ||
return | ||
} | ||
else -> { | ||
// Ignore all other requests. | ||
} | ||
} | ||
} | ||
|
||
private fun setUpMap() { | ||
if (checkSelfPermission(context as Activity, | ||
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { | ||
requestPermissions(arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), LOCATION_PERMISSION_REQUEST_CODE) | ||
return | ||
} | ||
|
||
map.isMyLocationEnabled = true | ||
|
||
fusedLocationClient.lastLocation.addOnSuccessListener(this.activity as Activity) { location -> | ||
// Got last known location. In some rare situations this can be null. | ||
if (location != null) { | ||
lastLocation = location | ||
val currentLatLng = LatLng(location.latitude, location.longitude) | ||
map.animateCamera(CameraUpdateFactory.newLatLngZoom(currentLatLng, 12f)) | ||
map.addMarker(MarkerOptions().position(currentLatLng).title("My location")) | ||
map.moveCamera(CameraUpdateFactory.newLatLng(currentLatLng)) | ||
} | ||
} | ||
} | ||
|
||
|
||
private val mapReadyCallback = OnMapReadyCallback { googleMap -> | ||
/** | ||
* Manipulates the map once available. | ||
* This callback is triggered when the map is ready to be used. | ||
* This is where we can add markers or lines, add listeners or move the camera. | ||
* In this case, we just add a marker near Sydney, Australia. | ||
* If Google Play services is not installed on the device, the user will be prompted to | ||
* install it inside the SupportMapFragment. This method will only be triggered once the | ||
* user has installed Google Play services and returned to the app. | ||
*/ | ||
map = googleMap | ||
map.uiSettings.isZoomControlsEnabled = true | ||
setUpMap() | ||
} | ||
|
||
override fun onCreateView( | ||
inflater: LayoutInflater, | ||
container: ViewGroup?, | ||
savedInstanceState: Bundle? | ||
): View? { | ||
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this.activity as Activity) | ||
return inflater.inflate(R.layout.fragment_maps, container, false) | ||
} | ||
|
||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
super.onViewCreated(view, savedInstanceState) | ||
val mapFragment = childFragmentManager.findFragmentById(R.id.map) as SupportMapFragment? | ||
mapFragment?.getMapAsync(mapReadyCallback) | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
android/canonical/app/src/main/java/com/google/samples/quickstart/canonical/MeFragment.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.google.samples.quickstart.canonical | ||
|
||
import android.os.Bundle | ||
import androidx.fragment.app.Fragment | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
|
||
// TODO: Rename parameter arguments, choose names that match | ||
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER | ||
private const val ARG_PARAM1 = "param1" | ||
private const val ARG_PARAM2 = "param2" | ||
|
||
/** | ||
* A simple [Fragment] subclass. | ||
* Use the [MeFragment.newInstance] factory method to | ||
* create an instance of this fragment. | ||
*/ | ||
class MeFragment : Fragment() { | ||
// TODO: Rename and change types of parameters | ||
private var param1: String? = null | ||
private var param2: String? = null | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
arguments?.let { | ||
param1 = it.getString(ARG_PARAM1) | ||
param2 = it.getString(ARG_PARAM2) | ||
} | ||
} | ||
|
||
override fun onCreateView( | ||
inflater: LayoutInflater, container: ViewGroup?, | ||
savedInstanceState: Bundle? | ||
): View? { | ||
// Inflate the layout for this fragment | ||
return inflater.inflate(R.layout.fragment_me, container, false) | ||
} | ||
|
||
companion object { | ||
/** | ||
* Use this factory method to create a new instance of | ||
* this fragment using the provided parameters. | ||
* | ||
* @param param1 Parameter 1. | ||
* @param param2 Parameter 2. | ||
* @return A new instance of fragment MeFragment. | ||
*/ | ||
// TODO: Rename and change types and number of parameters | ||
@JvmStatic | ||
fun newInstance(param1: String, param2: String) = | ||
MeFragment().apply { | ||
arguments = Bundle().apply { | ||
putString(ARG_PARAM1, param1) | ||
putString(ARG_PARAM2, param2) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.