获取谷歌地图android中可见区域的邮政编码列表

时间:2018-10-03 06:25:20

标签: android google-maps kotlin geocoder

希望您在自己的领域做得很好。

需要一些帮助,以便从Google地图的用户可见区域获取邮政编码列表,因为我得到了可见区域。 reference link for getting visible region

现在,我正在尝试使用Geocoder和可见区域值来获取地址,但出现异常。

在这里,我将显示我的代码。[在Kotlin工作]

MainActivity.kt

import android.Manifest
import android.annotation.SuppressLint
import android.content.pm.PackageManager
import android.location.Geocoder
import android.os.Bundle
import android.support.v4.app.ActivityCompat
import android.support.v4.content.ContextCompat
import android.support.v7.app.AppCompatActivity
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


class MapActivity : AppCompatActivity(), OnMapReadyCallback {

    var map: SupportMapFragment? = null
    var googleMap: GoogleMap? = null
    var MY_LOCATION_REQUEST_CODE: Int = 201

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_map)
    }


    override fun onResume() {
        super.onResume()
        if (map == null) {
            map = supportFragmentManager.findFragmentById(R.id.map) as SupportMapFragment?
            map!!.getMapAsync(this)
        }
    }

    override fun onMapReady(googleMap: GoogleMap?) {
        this.googleMap = googleMap

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                == PackageManager.PERMISSION_GRANTED) {

            googleMap!!.isMyLocationEnabled = true

            this.googleMap!!.animateCamera(CameraUpdateFactory.newLatLngZoom(LatLng(37.422, -122.084), 14.0f))

            getAddressListfromVisibleRegion()

        } else {
            ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), MY_LOCATION_REQUEST_CODE)
        }
    }

    private fun getAddressListfromVisibleRegion() {
        var vregion = googleMap!!.projection.visibleRegion.latLngBounds

        var addressList = Geocoder(this).getFromLocationName("", 100,
                vregion.southwest.latitude, vregion.southwest.longitude,
                vregion.northeast.latitude, vregion.northeast.longitude)

        /*

        val ne = vregion.northeast // LatLng of the north-east corner
        val sw = vregion.southwest // LatLng of the south-west corner
        val nw = LatLng(ne.latitude, sw.longitude)
        val se = LatLng(sw.latitude, ne.longitude)

        var addressList1 = Geocoder(this).getFromLocationName("", 100,
                nw.latitude, nw.longitude, se.latitude, se.longitude)

         */
    }


    @SuppressLint("MissingPermission")
    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        if (requestCode == MY_LOCATION_REQUEST_CODE) {
            if (permissions.size == 1 &&
                    permissions[0] == Manifest.permission.ACCESS_FINE_LOCATION &&
                    grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                googleMap!!.isMyLocationEnabled = true
                this.googleMap!!.animateCamera(CameraUpdateFactory.newLatLngZoom(LatLng(37.422, -122.084), 14.0f))
            }
        }
    }
}

实施Geocoder时遇到异常

enter image description here

我被引荐this链接来解决上述问题,并通过真实设备进行了检查,但在执行Geocoder语句时仍然遇到相同的问题。

任何人都想与我分享知识,这将对我有帮助

谢谢。

0 个答案:

没有答案