Skip to content

Commit 3f6c7d3

Browse files
author
roman_tcaregorodtcev
committed
Zoom and ViewType for MapIntentBuilder added
1 parent 6ee64b7 commit 3f6c7d3

3 files changed

Lines changed: 115 additions & 62 deletions

File tree

core/src/main/java/com/omega_r/libs/omegaintentbuilder/builders/MapIntentBuilder.kt

Lines changed: 99 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import com.omega_r.libs.omegaintentbuilder.handlers.FailCallback
2323
import com.omega_r.libs.omegaintentbuilder.interfaces.IntentHandler
2424
import com.omega_r.libs.omegaintentbuilder.types.MapTypes
2525
import com.omega_r.libs.omegaintentbuilder.types.MapTypes.*
26+
import com.omega_r.libs.omegaintentbuilder.types.MapViewTypes
27+
import com.omega_r.libs.omegaintentbuilder.types.MapViewTypes.*
2628

2729
/**
2830
* MapIntentBuilder is a helper for open Maps applications
@@ -33,6 +35,8 @@ class MapIntentBuilder(private vararg var types: MapTypes) : BaseActivityBuilder
3335
private var longitude: Double? = null
3436
private var address: String? = null
3537
private var failtype: MapTypes? = null
38+
private var viewType: MapViewTypes? = null
39+
private var zoom: Int? = null
3640

3741
init {
3842
if (types.isEmpty()) types = MapTypes.values()
@@ -89,58 +93,109 @@ class MapIntentBuilder(private vararg var types: MapTypes) : BaseActivityBuilder
8993
return this
9094
}
9195

96+
fun zoom(zoom: Int): MapIntentBuilder {
97+
if (zoom >= 0) this.zoom = zoom
98+
return this
99+
}
100+
101+
fun viewType(viewType: MapViewTypes): MapIntentBuilder {
102+
this.viewType = viewType
103+
return this
104+
}
105+
92106
override fun createIntent(context: Context): Intent {
93-
val uri: Uri = getFormattedUri(0)
107+
// We take first because next type will be handled in WrapperIntentHandler
108+
val mapType = types.first()
109+
val uri: Uri = getFormattedUri(mapType)
94110
val intent = Intent(Intent.ACTION_VIEW, uri)
95111

96-
when (types[0]) {
97-
GOOGLE_MAP -> intent.setPackage(GOOGLE_MAP.packageName)
98-
YANDEX_MAP -> intent.setPackage(YANDEX_MAP.packageName)
99-
KAKAO_MAP -> intent.setPackage(KAKAO_MAP.packageName)
100-
NAVER_MAP -> intent.setPackage(NAVER_MAP.packageName)
101-
}
112+
when (mapType) {
113+
GOOGLE_MAP -> GOOGLE_MAP.packageName
114+
YANDEX_MAP -> YANDEX_MAP.packageName
115+
KAKAO_MAP -> KAKAO_MAP.packageName
116+
NAVER_MAP -> NAVER_MAP.packageName
117+
}.also { intent.setPackage(it) }
102118

103119
return intent
104120
}
105121

106-
private fun getFormattedUri(index: Int): Uri {
122+
private fun getFormattedUri(mapType: MapTypes): Uri {
107123
val sb = StringBuilder()
108-
when (types[index]) {
109-
GOOGLE_MAP -> {
110-
sb.append("geo:")
111-
if (latitude != null && longitude != null) {
112-
sb.append(latitude, ",", longitude)
113-
}
114-
if (address != null) {
115-
sb.append("?q=", Uri.encode(address))
116-
}
117-
}
118-
YANDEX_MAP -> {
119-
sb.append("yandexmaps://", YANDEX_MAP.packageName, "/?pt=")
120-
if (latitude != null && longitude != null) {
121-
sb.append(longitude, ",", latitude)
122-
}
123-
if (address != null) {
124-
sb.append("&text=", address)
125-
}
126-
}
127-
KAKAO_MAP -> {
128-
sb.append("daummaps://look?p=")
129-
if (latitude != null && longitude != null) {
130-
sb.append(latitude, ",", longitude)
131-
}
124+
when(mapType) {
125+
GOOGLE_MAP -> formulateGoogleUri(sb)
126+
YANDEX_MAP -> formulateYandexMapUri(sb)
127+
KAKAO_MAP -> formulateKakaoMapUri(sb)
128+
NAVER_MAP -> formulateNaverMapUri(sb)
129+
}
130+
return Uri.parse(sb.toString())
131+
}
132+
133+
private fun formulateGoogleUri(sb: StringBuilder) {
134+
val viewType = when (viewType) {
135+
MAP -> "m"
136+
SATELLITE -> "k"
137+
HYBRID -> "h"
138+
TERRAIN -> "p"
139+
GOOGLE_EARTH -> "e"
140+
else -> null
141+
}
142+
143+
if (viewType == null) {
144+
sb.append("geo:")
145+
if (latitude != null && longitude != null) {
146+
sb.append(latitude, ",", longitude)
132147
}
133-
NAVER_MAP -> {
134-
sb.append("geo:")
135-
if (latitude != null && longitude != null) {
136-
sb.append(latitude, ",", longitude)
137-
}
138-
if (address != null) {
139-
sb.append("?q=", Uri.encode(address))
140-
}
148+
address?.let { sb.append("?q=", Uri.encode(address)) }
149+
} else {
150+
sb.append("http://maps.google.com/maps?")
151+
.append("t=$viewType")
152+
address?.let { sb.append("&q=", Uri.encode(address)) }
153+
if (latitude != null && longitude != null) {
154+
sb.append("&loc:")
155+
.append(latitude, "+", longitude)
141156
}
142157
}
143-
return Uri.parse(sb.toString())
158+
}
159+
160+
private fun formulateYandexMapUri(sb: StringBuilder) {
161+
sb.append("yandexmaps://", "maps.yandex.ru/?")
162+
if (latitude != null && longitude != null) {
163+
sb.append("pt=")
164+
.append(longitude, ",", latitude)
165+
}
166+
val viewType = when (viewType) {
167+
MAP -> "map"
168+
SATELLITE -> "sat"
169+
HYBRID -> "skl"
170+
OPEN_MAP -> "pmap"
171+
else -> null
172+
}
173+
viewType?.let { sb.append("&l=", viewType) }
174+
zoom?.let { sb.append("&z=", zoom) }
175+
address?.let { sb.append("&text=", Uri.encode(address)) }
176+
}
177+
178+
private fun formulateNaverMapUri(sb: StringBuilder) {
179+
sb.append("nmap://")
180+
if (address == null) sb.append("map?") else sb.append("place?")
181+
latitude?.let { sb.append("lat=$latitude") }
182+
longitude?.let { sb.append("&lng=$longitude") }
183+
zoom?.let { sb.append("&zoom=$zoom") }
184+
address?.let { sb.append("&name=", Uri.encode(address)) }
185+
}
186+
187+
private fun formulateKakaoMapUri(sb: StringBuilder) {
188+
sb.append("daummaps://")
189+
if (address == null) {
190+
sb.append("look?")
191+
} else {
192+
sb.append("search?")
193+
.append("q=", address, "&")
194+
}
195+
if (latitude != null && longitude != null) {
196+
sb.append("p=")
197+
.append(latitude, ",", longitude)
198+
}
144199
}
145200

146201
override fun createIntentHandler(activity: Activity): IntentHandler {
@@ -152,7 +207,7 @@ class MapIntentBuilder(private vararg var types: MapTypes) : BaseActivityBuilder
152207
}
153208

154209
override fun createIntentHandler(fragment: androidx.fragment.app.Fragment): IntentHandler {
155-
return fragment.context?.let { createFailIntentHandler(it) } ?: super.createIntentHandler(fragment)
210+
return fragment.context?.let { createFailIntentHandler(it) } ?: super.createIntentHandler(fragment)
156211
}
157212

158213
override fun createIntentHandler(context: Context): IntentHandler {
@@ -176,6 +231,8 @@ class MapIntentBuilder(private vararg var types: MapTypes) : BaseActivityBuilder
176231
it.address = address
177232
it.latitude = latitude
178233
it.longitude = longitude
234+
it.zoom = zoom
235+
it.viewType = viewType
179236
}
180237
.createIntentHandler(context)
181238
result += intentHandler
@@ -250,22 +307,6 @@ class MapIntentBuilder(private vararg var types: MapTypes) : BaseActivityBuilder
250307
return this
251308
}
252309

253-
override fun startActivity() {
254-
handler.startActivity()
255-
}
256-
257-
override fun startActivityForResult(requestCode: Int, options: Bundle?) {
258-
handler.startActivityForResult(requestCode, options)
259-
}
260-
261-
override fun startActivityForResult(callback: ActivityResultCallback) {
262-
handler.startActivityForResult(callback)
263-
}
264-
265-
override fun getIntent(): Intent {
266-
return handler.getIntent()
267-
}
268-
269310
override fun addFlagsClearBackStack(): IntentHandler {
270311
handler.addFlagsClearBackStack()
271312
return this

core/src/main/java/com/omega_r/libs/omegaintentbuilder/types/MapTypes.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ package com.omega_r.libs.omegaintentbuilder.types
22

33
enum class MapTypes(val packageName: String) {
44

5-
GOOGLE_MAP("com.google.android.apps.maps"),
6-
YANDEX_MAP("ru.yandex.yandexmaps"),
7-
KAKAO_MAP("net.daum.android.map"),
8-
NAVER_MAP("com.nhn.android.nmap")
5+
GOOGLE_MAP("com.google.android.apps.maps"),
6+
YANDEX_MAP("ru.yandex.yandexmaps"),
7+
KAKAO_MAP("net.daum.android.map"),
8+
NAVER_MAP("com.nhn.android.nmap")
99

1010
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.omega_r.libs.omegaintentbuilder.types
2+
3+
enum class MapViewTypes {
4+
5+
MAP,
6+
SATELLITE,
7+
HYBRID,
8+
TERRAIN,
9+
GOOGLE_EARTH,
10+
OPEN_MAP
11+
12+
}

0 commit comments

Comments
 (0)