Created
May 22, 2018 14:36
-
-
Save PattyAppier/f388b22ad9e6f7c822ec789f33278359 to your computer and use it in GitHub Desktop.
PattysPockemonApp(AndroidStudio.Kotlin)
This file contains 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
package com.example.pattychen.pattyspockemon | |
import android.content.Context | |
import android.content.pm.PackageManager | |
import android.location.Location | |
import android.location.LocationListener | |
import android.location.LocationManager | |
import android.os.Build | |
import android.support.v4.app.FragmentActivity | |
import android.os.Bundle | |
import android.support.v4.app.ActivityCompat | |
import android.util.Log | |
import android.widget.Toast | |
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.BitmapDescriptorFactory | |
import com.google.android.gms.maps.model.LatLng | |
import com.google.android.gms.maps.model.MarkerOptions | |
class MapsActivity : FragmentActivity(), OnMapReadyCallback { | |
private var mMap: GoogleMap? = null | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_maps) | |
// Obtain the SupportMapFragment and get notified when the map is ready to be used. | |
val mapFragment = supportFragmentManager | |
.findFragmentById(R.id.map) as SupportMapFragment | |
mapFragment.getMapAsync(this) | |
checkPermmison() | |
LoadPockemon() | |
} | |
var ACCESSLOCATION=123 | |
fun checkPermmison(){ | |
if(Build.VERSION.SDK_INT>=23){ | |
if(ActivityCompat. | |
checkSelfPermission(this, | |
android.Manifest.permission.ACCESS_FINE_LOCATION)!=PackageManager.PERMISSION_GRANTED){ | |
requestPermissions(arrayOf(android.Manifest.permission.ACCESS_FINE_LOCATION),ACCESSLOCATION) | |
return | |
} | |
} | |
GetUserLocation() | |
} | |
fun GetUserLocation(){ | |
Toast.makeText(this,"User location access on",Toast.LENGTH_LONG).show() | |
//TODO: Will implement later | |
var myLocation= MylocationListener() | |
var locationManager=getSystemService(Context.LOCATION_SERVICE) as LocationManager | |
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,3,3f,myLocation) | |
var mythread=myThread() | |
mythread.start() | |
} | |
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) { | |
when(requestCode){ | |
ACCESSLOCATION->{ | |
if (grantResults[0]==PackageManager.PERMISSION_GRANTED){ | |
GetUserLocation() | |
}else{ | |
Toast.makeText(this,"We cannot access to your location",Toast.LENGTH_LONG).show() | |
} | |
} | |
} | |
super.onRequestPermissionsResult(requestCode, permissions, grantResults) | |
} | |
override fun onMapReady(googleMap: GoogleMap) { | |
mMap = googleMap | |
} | |
var location:Location?=null | |
//Get user location | |
inner class MylocationListener:LocationListener{ | |
constructor(){ | |
location= Location("Start") | |
location!!.longitude=0.0 | |
location!!.longitude=0.0 | |
} | |
override fun onLocationChanged(p0: Location?) { | |
location=p0 | |
} | |
override fun onStatusChanged(p0: String?, p1: Int, p2: Bundle?) { | |
//TODO("not implemented") //To change body of created functions use File | Settings | File Templates. | |
} | |
override fun onProviderEnabled(p0: String?) { | |
// TODO("not implemented") //To change body of created functions use File | Settings | File Templates. | |
} | |
override fun onProviderDisabled(p0: String?) { | |
//TODO("not implemented") //To change body of created functions use File | Settings | File Templates. | |
} | |
} | |
var oldLocation:Location?=null | |
inner class myThread:Thread{ | |
constructor():super(){ | |
oldLocation= Location("Start") | |
oldLocation!!.longitude=0.0 | |
oldLocation!!.longitude=0.0 | |
} | |
override fun run(){ | |
while (true){ | |
try { | |
if(oldLocation!!.distanceTo(location)==0f){ | |
continue | |
} | |
oldLocation=location | |
runOnUiThread { | |
mMap!!.clear() | |
// show me | |
val sydney = LatLng(location!!.latitude, location!!.longitude) | |
mMap!!.addMarker(MarkerOptions() | |
.position(sydney) | |
.title("Me") | |
.snippet(" here is my location") | |
.icon(BitmapDescriptorFactory.fromResource(R.drawable.mario))) | |
mMap!!.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 14f)) | |
// show pockemons | |
for(i in 0..listPockemons.size-1){ | |
var newPockemon=listPockemons[i] | |
if(newPockemon.IsCatch==false){ | |
val pockemonLoc = LatLng(newPockemon.location!!.latitude, newPockemon.location!!.longitude) | |
mMap!!.addMarker(MarkerOptions() | |
.position(pockemonLoc) | |
.title(newPockemon.name!!) | |
.snippet(newPockemon.des!! +", power:"+ newPockemon!!.power) | |
.icon(BitmapDescriptorFactory.fromResource(newPockemon.image!!))) | |
if (location!!.distanceTo(newPockemon.location)<2){ | |
newPockemon.IsCatch=true | |
listPockemons[i]=newPockemon | |
playerPower+=newPockemon.power!! | |
Toast.makeText(applicationContext, | |
"You catch new pockemon your new pwoer is " + playerPower, | |
Toast.LENGTH_LONG).show() | |
} | |
} | |
} | |
} | |
Thread.sleep(1000) | |
}catch (ex:Exception){} | |
} | |
} | |
} | |
var playerPower=0.0 | |
var listPockemons=ArrayList<Pockemon>() | |
fun LoadPockemon(){ | |
listPockemons.add(Pockemon(R.drawable.charmander, | |
"Charmander", "Charmander living in japan", 55.0, 25.059957, 121.572320)) | |
listPockemons.add(Pockemon(R.drawable.bulbasaur, | |
"Bulbasaur", "Bulbasaur living in usa", 90.5, 25.082133, 121.567213)) | |
listPockemons.add(Pockemon(R.drawable.squirtle, | |
"Squirtle", "Squirtle living in iraq", 33.5, 25.049287, 121.578272)) | |
} | |
} |
This file contains 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
package com.example.pattychen.pattyspockemon | |
import android.location.Location | |
class Pockemon{ | |
var name:String?=null | |
var des:String?=null | |
var image:Int?=null | |
var power:Double?=null | |
var location:Location?=null | |
var IsCatch:Boolean?=false | |
constructor(image:Int,name:String,des:String,power:Double,lat:Double,log:Double){ | |
this.name=name | |
this.des=des | |
this.image=image | |
this.power=power | |
this.location= Location(name) | |
this.location!!.latitude=lat | |
this.location!!.longitude=log | |
this.IsCatch=false | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment