Pockemon Go转向空地图

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

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

Pockemon.tkt

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

AndroidManifest

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="ahmedchtn.pockemontn"> <!-- The ACCESS_COARSE/FINE_LOCATION permissions are not required to use Google Maps Android API v2, but you must specify either coarse or fine location permissions for the 'MyLocation' functionality. --> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <!-- The API key for Google Maps-based APIs is defined as a string resource. (See the file "res/values/google_maps_api.xml"). Note that the API key is linked to the encryption key used to sign the APK. You need a different API key for each encryption key, including the release key that is used to sign the APK for publishing. You can define the keys for the debug and release targets in src/debug/ and src/release/. --> <meta-data android:name="com.google.android.geo.API_KEY" android:value="@string/google_maps_key" /> <activity android:name=".MapsActivity" android:label="@string/title_activity_maps"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> 

提前致谢 !

在设置中检查您的权限 – 如果应用程序没有使用您的位置的权限,它可能显示为空白。

  1. 获取Google Map API密钥。
  2. 确保在创建应用程序时选择地图模板。
  3. 创建应用程序后,将您的应用程序从调试更改为发布。
  4. 现在转到google_maps_api.xml并将您的API密钥插入文件中的指定药水,告诉您在那里替换“ google_maps_key ”。

重建并运行你的应用程序。 地图应该现在显示。

要显示Pokemons,你的LoadPokemon()函数调用应该在GetUserLocation()后面的checkPermission(){}函数中,而不是在onCreate()函数中。