Pockemon Go转向空地图

时间:2017-06-17 15:23:31

标签: android intellij-idea kotlin

我正在使用与Pokemon Go类似的Kotlin语言开发Android App, 应用程序中没有问题但是当我在手机中安装它时,只显示一个空白地图并且该人没有去我的位置我该如何解决?

MapsActivity.tkt

SB=''
import random

N1=[0 for x in range(5)]
for i in range(5):
    N1[i]=random.randrange(1, 13)

N2=[0 for x in range(5)]
for i in range(5):
    N2[i]=random.randrange(5, 16)

ANS=[0 for x in range(5)]
C=[0 for x in range(5)]
for i in range(5):
    C[i]=N1[i]*N2[i]
    print(N1[i],'x',N2[i])
    ANS[i]=int(input('Enter an Anwser:'))
    if ANS[i]==C[i]:
        print('Correct')
    elif ANS[i]!=C[i]:
        print('Incorrect')
    print(SB)

print('Correct Anwsers')
for i in range(5):
    if ANS[i]==C[i]:
        print(N1[i],'x',N2[i], '=', ANS[i])

print('Incorrect Anwsers')
for i in range(5):
    if ANS[i]==C[i]:
        print(N1[i],'x',N2[i],'=',ANS[i])

Pockemon.tkt

package ahmedchtn.pockemontn

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.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.BitmapDescriptor
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 {

    //WORK WITH USER LOCATION


    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)
    }


    /**
     * 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.
     */
    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.naruto)))
                        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.charmandertn,
                "Charmander", "Charmander living in japan", 55.0, 35.687997, 10.085267))
        listPockemons.add(Pockemon(R.drawable.bulbasaurtn,
                "Bulbasaur", "Bulbasaur living in usa", 90.5, 35.687657, 10.084838))
        listPockemons.add(Pockemon(R.drawable.squirtletn,
                "Squirtle", "Squirtle living in iraq", 33.5, 35.687552, 10.084623))

    }

}

AndroidManifest

package ahmedchtn.pockemontn

import android.location.Location

/**
 * Created by Ahmed on 17-06-2017.
 */

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
    }

}

提前致谢!

2 个答案:

答案 0 :(得分:0)

在设置中检查您的权限 - 如果该应用无权使用您的位置,则可能会显示空白。

答案 1 :(得分:0)

  1. 获取Google Map Api密钥。
  2. 确保在创建应用时选择地图模板。
  3. 创建应用后,将应用从调试更改为发布。
  4. 现在转到google_maps_api.xml并将您的api密钥插入到 文件中指定的药水,告诉您更换 &#34; google_maps_key&#34;那里。
  5. 重建并运行您的应用。地图应该现在显示。

    要制作Pokemons节目,您的LoadPokemon()函数调用应位于checkPermission(){}之后的GetUserLocation()函数中,而不应位于onCreate()函数中。