2017-06-17 21 views
-2

我正在使用Kotlin的Android應用程序使用與Pokemon Go類似的語言, 應用程序中沒有問題,但是當我將它安裝在手機中時,只顯示空白地圖,人不會去我的位置我如何解決這個問題?Pockemon轉到空地圖

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> 

提前感謝!

回答

0

在設置中檢查您的權限 - 如果應用程序沒有使用您的位置的權限,則它可能顯示爲空白。

+0

我已經在我的問題剛剛添加了AndroidMAnifest – tuniprocoder

0
  1. 獲取Google Map API密鑰。
  2. 確保您在創建應用時選擇地圖模板。
  3. 創建應用程序後,將您的應用程序從調試更改爲發佈。
  4. 現在轉到google_maps_api.xml,並在文件中將您的API密鑰插入 指定的位置,以告訴您在那裏替換 「google_maps_key」。

重建並運行您的應用程序。地圖應該現在顯示。

爲了讓小寵物秀,你LoadPokemon()函數調用應在checkPermission(){}功能GetUserLocation()之後,而不是在onCreate()功能。

+0

我已經做到了,地圖顯示,但口袋妖怪不顯示播放器 – tuniprocoder

+0

我發現你的代碼有問題。儘快更新我的答案。 –

+0

好的Alf Moh,謝謝,我在等你 – tuniprocoder