1

I hope you are doing good in your field.

Need some help for getting list of postal codes from the user visible area of google map for that i got the visible region. reference link for getting visible region

Now i am trying to get the address using Geocoder and visible region value but i get the exception.

Here i will show my code.[ Worked in 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))
            }
        }
    }
}

Got the exception when implement Geocoder

enter image description here

I was refered this link for above issue and also checked with real device but still face same issue when execute Geocoder statement.

Anyone want to share knowledge with me that will be good help for me

Thanks in advance.

Nilesh Panchal
  • 1,059
  • 1
  • 10
  • 24
  • I don't understand how you plan to get all the ZIP codes. If you cover a 20-30 square block region of Manhattan, there could be dozens of ZIP codes. Does Google expose an API for doing this? – Tim Biegeleisen Oct 03 '18 at 06:28
  • yes @Tim Biegeleisen you are right we have to create boundary box using latLngBounds so we have the boundary point now using that point we are getting list full address from that boundary area using Geocoder. – Nilesh Panchal Oct 03 '18 at 09:12
  • Just keep in mind that US ZIP codes can be divided in very uneven ways. My doubt here is how you would know what the bounds of a given ZIP code are a priori. – Tim Biegeleisen Oct 03 '18 at 09:13
  • My requirement is only that getting the zip codes from the google map visible area. i am not getting to display any zip code area and priori is not required. – Nilesh Panchal Oct 03 '18 at 09:21
  • Take a look at [this](https://gis.stackexchange.com/q/2682/87666) question and answers. Also take a look at [that](https://help.openstreetmap.org/questions/46371/need-help-showing-zip-codes-on-map-and-their-boundaries). – Andrii Omelchenko Oct 03 '18 at 09:46

0 Answers0