如何从Google Maps扩展共享位置的URL? [英] How to expand the shared location URL from Google Maps?
问题描述
我正在尝试从Google Map共享位置链接获取位置,因此我正在使用Google Shorten URL API来扩展URL,但是当我从android设备从Google Map应用程序共享URL时,它会给我https://maps.app.goo.gl/eEhh3
这种网址.它没有给我实际的扩展URL以提供位置信息.
I am trying to get the location from google map shared location link so I am using Google Shorten URL API to expand the URL but when I shared URL from Google Map application from the android device it gives me https://maps.app.goo.gl/eEhh3
this kind of URL. It does not give me actual expanded URL to provide Location information.
如何对此进行扩展:
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 .
Here is the API URL which I am using to expand it.
"https://www.googleapis.com/urlshortener/v1/url?shortUrl=https://maps.app.goo.gl/eEhh3&key=(API_KEY)
"
但是,没有给我上面的位置信息URL,而是给了我这个长URL .
But instead of giving me the above location information URL it returns me this Long 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
请帮助我如何通过Google API获取上述位置信息URL,以便从任意设备上通过Google Map共享位置网址获取位置(拉特,Lng)(IOS,WebAPP ,特别是Android).
Please help me how can I get above-mentioned Location Info URL by Google API to get Location (Lat, Lng) through Google Map shared location Url from any device (IOS, WebAPP, Specifically Android).
推荐答案
现在(在2019年)似乎没有单独的API或服务来扩展Google Maps的共享位置URL,但是您可以使用基于该技巧的变通方法:Google服务器可以通过Google Maps Web界面执行此操作.
Seems now (in 2019) there is no separate API or service for expanding shared location URL from Google Maps, but you can use workaround based on that trick: Google server can do it via Google Maps web interface.
TLDR;
您不需要扩展API .例如,如果
https://maps.app.goo.gl/eEhh3
在PC上的网络浏览器(例如FireFox)中键入的URL会扩展该URL并显示标记.在此期间,您可以在地址字段中看到部分扩展" URL,例如:
URL typed in the web browser on PC (e.g. FireFox) than that URL was expanded and the marker was shown. And during that you can see in address field "partially expanded" URL like:
https://www.google.com/maps/place//data=!4m2!3m1!1s0x391904e4af9b1e35:0xee6f3c848c9e5341?utm_source=mstt_1
其在1-2秒内被具有lat/lng坐标的完全扩展" URL更改(重定向到),例如:
which in 1-2 sec changed (redirected to) by "full expanded" URL with lat/lng coordinates like:
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
.
因此,您可以使用 WebView
进行操作并获取从中完全展开"(例如,在public void onPageFinished(WebView view, String url)
"rel =" nofollow noreferrer> WebViewClient
).注意:有必要跳过第一个部分扩展"的URL.
So you can use WebView
to do the same and get "full expanded" from it (e.g. in public void onPageFinished(WebView view, String url)
of WebViewClient
). NB: it's necessary to skip first "partially expanded" URL.
另一个问题:WebView
将缩短的URL(例如https://maps.app.goo.gl/eEhh3
)转换为Google Maps Application的Intent和部分扩展"的URL
https://www.google.com/maps/place//data=!4m2!3m1!1s0x391904e4af9b1e35:0xee6f3c848c9e5341?utm_source=mstt_1
永远不会被加载.实际上,如果只需要显示标记就足够了.但是,如果您需要坐标,则可以通过HttpUrlConnection.getURL()
或打开连接并 HttpUrlConnection.getInputStream()
.实际上,如果将来自HttpUrlConnection
输入流的所有响应加载为String
-您可以通过https://www.google.com/maps/preview/place
标签在其中找到所需的经/纬度坐标.像这样:
The other issue: WebView
converts shortened URL like https://maps.app.goo.gl/eEhh3
into Intent for Google Maps Application and "partially expanded" URL
https://www.google.com/maps/place//data=!4m2!3m1!1s0x391904e4af9b1e35:0xee6f3c848c9e5341?utm_source=mstt_1
never be loaded. Actually it's enough if you need just show marker. But if you need coordinates this issue can be solved if you open shortened URL https://maps.app.goo.gl/eEhh3
by HttpUrlConnection
and get redirected "partially expanded" URL via HttpUrlConnection.getURL()
or via Location
Header field after connection opened and HttpUrlConnection.getInputStream()
called. Actually, if you load all response from HttpUrlConnection
input stream as String
- you can find desired lat/lng coordinates within it by https://www.google.com/maps/preview/place
tag. Something like:
...
[[[1,42]\n]\n,0,null,0,47]\n]\n,null,\"Suit 706 Siddiq Trade Center, Main Blvd Gulberg, Block H Gulberg III, Lahore, Punjab, Pakistan\",null,null,\"https://www.google.com/maps/preview/place/Siddique+Trade+Center,+Suit+706+Siddiq+Trade+Center,+Main+Blvd+Gulberg,+Block+H+Gulberg+III,+Lahore,+Punjab,+Pakistan/@31.5313297,74.3526346,3401a,13.1y/data\\u003d!4m2!3m1!1s0x391904e4af9b1e35:0xee6f3c848c9e5341\",1,null,null,null,null,null,
...
通过HttpUrlConnection
打开的缩短的URL.恕我直言,更好(但稍微慢一点)的方法是传递从 HttpUrlConnection.getURL()
插入WebView
并从其地址行获取完全展开"的URL.因此,具有完整的源代码:
...
[[[1,42]\n]\n,0,null,0,47]\n]\n,null,\"Suit 706 Siddiq Trade Center, Main Blvd Gulberg, Block H Gulberg III, Lahore, Punjab, Pakistan\",null,null,\"https://www.google.com/maps/preview/place/Siddique+Trade+Center,+Suit+706+Siddiq+Trade+Center,+Main+Blvd+Gulberg,+Block+H+Gulberg+III,+Lahore,+Punjab,+Pakistan/@31.5313297,74.3526346,3401a,13.1y/data\\u003d!4m2!3m1!1s0x391904e4af9b1e35:0xee6f3c848c9e5341\",1,null,null,null,null,null,
...
for your shortened URL opened via HttpUrlConnection
. IMHO better (but slightly slower) way is to pass "partially expanded" URL received from HttpUrlConnection.getURL()
into WebView
and get "full expanded" URL from its address line. So, with full source code:
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);
}
}
}
和activity_main.xml
:
<?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>
您可以获得类似的内容:
you can get something like this:
NB! Google可以随时更改描述的行为.
NB! Described behavior can be changed by Google at any time.
这篇关于如何从Google Maps扩展共享位置的URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!