Android 如何从谷歌地图扩展共享位置URL?
我试图从谷歌地图共享位置链接获取位置,因此我使用谷歌缩短URL API来扩展URL,但当我从android设备共享谷歌地图应用程序的URL时,它会给我Android 如何从谷歌地图扩展共享位置URL?,android,google-maps,google-maps-api-3,geolocation,maps,Android,Google Maps,Google Maps Api 3,Geolocation,Maps,我试图从谷歌地图共享位置链接获取位置,因此我使用谷歌缩短URL API来扩展URL,但当我从android设备共享谷歌地图应用程序的URL时,它会给我https://maps.app.goo.gl/eEhh3这种URL。它没有给我实际的扩展URL来提供位置信息 如何扩展此功能: https://maps.app.goo.gl/eEhh3 在此链接中: https://www.google.com/maps/place/Siddique+Trade+Center/@31.5313297,74.35
https://maps.app.goo.gl/eEhh3
这种URL。它没有给我实际的扩展URL来提供位置信息
如何扩展此功能:
https://maps.app.goo.gl/eEhh3
在此链接中:
https://www.google.com/maps/place/Siddique+Trade+Center/@31.5313297,74.3504459,17z/data=!3m1!4b1!4m5!3m4!1s0x391904e4af9b1e35:0xee6f3c848c9e5341!8m2!3d31.5313297!4d74.3526346
下面是我用来扩展它的API URL
“https://www.googleapis.com/urlshortener/v1/url?shortUrl=https://maps.app.goo.gl/eEhh3&key=(API_键)
“
但是它没有给我上面的位置信息URL,而是返回这个长URL
https://maps.app.goo.gl/?link=https://www.google.com/maps/place//data%3D!4m2!3m1!1s0x391904e4af9b1e35:0xee6f3c848c9e5341?utm_source%3Dmstt_1&apn=com.google.android.apps.maps&ibi=com.google.Maps&ius=comgooglemapsurl&isi=585027354
请帮助我如何通过谷歌API获取上述位置信息URL,以通过任何设备(IOS、WebAPP,特别是Android)的谷歌地图共享位置URL获取位置(Lat、Lng)。现在(2019年)似乎没有单独的API或服务用于从谷歌地图扩展共享位置URL,但您可以使用基于该技巧的变通方法:谷歌服务器可以通过谷歌地图web界面来实现
TLDR强>
你不需要。例如,如果
https://maps.app.goo.gl/eEhh3
在PC(如FireFox)上的web浏览器中键入的URL比扩展的URL大,并显示标记。在此期间,您可以在地址字段中看到“部分扩展”URL,如:
https://www.google.com/maps/place//data=!4m2!3m1!10x391904E4AF9B1E35:0xee6f3c848c9e5341?utm_source=mstt_1
在1-2秒内,通过“完全扩展”URL更改(重定向到),并带有lat/lng坐标,如:
https://www.google.com/maps/place/Siddique+贸易+中心/@31.5313297,74.3504459,17z/数据=!3m1!4b1!4m5!3m4!10x391904E4AF9B1E35:0xee6f3c848c9e5341!8m2!3d31.5313297!4d74.3526346
因此,您可以使用来执行相同的操作,并从中获得“完全扩展”(例如,在of中)。注意:有必要跳过第一个“部分扩展”的URL
另一个问题:WebView
像https://maps.app.goo.gl/eEhh3
进入谷歌地图应用程序和“部分扩展”URL的意图
https://www.google.com/maps/place//data=!4m2!3m1!10x391904E4AF9B1E35:0xee6f3c848c9e5341?永远不要加载utm_source=mstt_1
。事实上,如果你只需要显示标记就足够了。但是如果您需要坐标,如果您打开缩短的URLhttps://maps.app.goo.gl/eEhh3
by,并在连接打开后通过或通过Location
标题字段重定向“部分扩展”URL。实际上,如果您将HttpUrlConnection
输入流中的所有响应作为String
加载,您可以通过https://www.google.com/maps/preview/place
tag。比如:
。。。
[[[1,42]\n]\n,0,null,0,47]\n,null,\“巴基斯坦旁遮普省拉合尔市古尔堡III区古尔堡主大道西迪克贸易中心706号西服\”,null,null\"https://www.google.com/maps/preview/place/Siddique+贸易+中心,+西服+706+Siddiq+贸易+中心,+Main+Blvd+Gulberg,+Block+H+Gulberg+III,+Lahore,+Punjab,+Pakistan/@31.5313297,74.35263463401a,13.1y/data\\u003d!4m2!3m1!10x391904E4AF9B1E35:0xee6f3c848c9e5341\”,1,null,null,null,null,
...
对于通过HttpUrlConnection
打开的缩短URL,更好(但稍微慢一点)的方法是将从接收到的“部分扩展”URL传递到WebView
,并从其地址行获取“完全扩展”URL。因此,完整源代码:
public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {
private GoogleMap mGoogleMap;
private MapFragment mMapFragment;
private WebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// configure WebView
System.setProperty("http.agent", ""); // reset default User-Agent
mWebView = (WebView) findViewById(R.id.web_view);
mWebView.setVisibility(View.GONE);
mWebView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
// skip non-"ful expanded" urls
if (isUrlFullExpanded(url)) {
// extract lat/lng coordinates from "full expanded" URL String
LatLng latLng = getLatLngFromExpandedUrl(url);
// just show marker with extracted coordinates for test
onLatLngReceived(latLng);
}
}
});
mWebView.getSettings().setJavaScriptEnabled(true);
mMapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map_fragment);
mMapFragment.getMapAsync(this);
// start get LatLng from URL coordinates task
new GetLocationURL().execute(new String[] {"https://maps.app.goo.gl/eEhh3"});
}
private LatLng getLatLngFromExpandedUrl(String url) {
final String beginMarker = "/@";
final String endMarker = "/";
final int ixCoordsStart = url.indexOf(beginMarker) + beginMarker.length();
final int ixCoordsEnd = url.indexOf(endMarker, ixCoordsStart);
String coordinatesString = url.substring(ixCoordsStart, ixCoordsEnd);
LatLng latLng = null;
if (!TextUtils.isEmpty(coordinatesString)) {
String[] coords = coordinatesString.split(",");
if (coords.length >= 2) {
latLng = new LatLng(Float.parseFloat(coords[0]), Float.parseFloat(coords[1]));
}
}
return latLng;
}
private boolean isUrlFullExpanded(String url) {
return url.indexOf("place/") > -1 && url.indexOf("place//") == -1 && url.indexOf("/@") > -1;
}
private void onLatLngReceived(LatLng latLng) {
if (mGoogleMap != null && latLng != null) {
mGoogleMap.addMarker(new MarkerOptions().position(latLng));
mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 16));
}
}
@Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;
}
private class GetLocationURL extends AsyncTask<String, Void, String> {
protected void onPreExecute() {
super.onPreExecute();
}
protected String doInBackground(String... params) {
String locationUrl = null;
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
URL url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
// you can analyze response code
//int responseCode = connection.getResponseCode();
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuilder responseStringBuilder = new StringBuilder();
String line = "";
while ((line = reader.readLine()) != null) {
responseStringBuilder.append(line);
}
// use commented code below if you want to get coordinates directly from HttpURLConnection response
/*
LatLng latLng = null;
String responseString = responseStringBuilder.toString();
String pattern = "\\\"https://www.google.com/maps/preview/place";
int ixPlaceStart = responseString.indexOf(pattern);
if (ixPlaceStart > -1) {
int ixCoordsStart = responseString.indexOf("@", ixPlaceStart) + 1;
int ixCoordsEnd = responseString.indexOf("/", ixCoordsStart);
String coordinatesString = responseString.substring(ixCoordsStart, ixCoordsEnd);
String[] coords = coordinatesString.split(",");
// latLng - coordinates from URL
latLng = new LatLng(Float.parseFloat(coords[0]), Float.parseFloat(coords[1]));
}
*/
locationUrl = connection.getURL().toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return locationUrl;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
// pass redirected "partially expandeded" URL to WebView
mWebView.loadUrl(result);
}
}
}
你可以得到这样的结果:
注意!谷歌可以随时更改所描述的行为。谢谢,我会研究你的答案。@不客气,但请记住-谷歌可以随时更改。我收到此URL
https://play.google.com/store/apps/details?id=com.google.android.apps.maps&pcampaignid=fdl_short&url=https://www.google.com/maps/place//data%3D!4m2!3m1!10x15c3d01fb1137e59:0xe0595579737b118db?utm_源%3Dmstt_1
而不是通过“HttpUrlConnection”的位置URL,我想谷歌方案已经改变了。当我在webView中点击这个URL时,它总是返回相同的URL。@TanveerMunir请检查webView
和http.agent
(System.setProperty(“http.agent”);
)设置。您的URL将转换为https://www.google.com/maps/place/Jeddah+沙特阿拉伯/@21.4498898,38.9309561,10z/数据=!3m1!4b1!4m5!3m4!1s0x15c3d01fb1137e59:0xe059579737b118db!8m2!3d21.485811!4d39.1925048
在我这边。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="<YOUR_PACKAGE>.MainActivity">
<fragment
android:id="@+id/map_fragment"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<WebView
android:id="@+id/web_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>