现在(2019 年)似乎没有单独的 API 或服务来扩展 Google 地图中的共享位置 URL,但您可以使用基于该技巧的解决方法:Google 服务器可以通过 Google 地图 Web 界面来完成此操作。
TLDR;
你不需要扩展API https://developers.google.com/url-shortener/v1/getting_started#expand。例如,如果
https://maps.app.goo.gl/eEhh3
在 PC 上的 Web 浏览器(例如 FireFox)中输入的 URL,该 URL 会展开并显示标记。在此期间,您可以在地址字段中看到“部分展开”的 URL,例如:
https://www.google.com/maps/place//data=!4m2!3m1!1s0x391904e4af9b1e35:0xee6f3c848c9e5341?utm_source=mstt_1
在 1-2 秒内通过“完全扩展”URL 更改(重定向到),其中纬度/经度坐标如下:
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 https://developer.android.com/reference/android/webkit/WebView做同样的事情并从中得到“完全扩展”(例如在public void onPageFinished(WebView view, String url) https://developer.android.com/reference/android/webkit/WebViewClient#onPageFinished(android.webkit.WebView,%20java.lang.String) of WebViewClient https://developer.android.com/reference/android/webkit/WebViewClient)。注意:有必要跳过第一个“部分扩展”的 URL。
另一个问题:WebView
将缩短的 URL 转换为https://maps.app.goo.gl/eEhh3
进入 Google 地图应用程序的 Intent 和“部分扩展”URLhttps://www.google.com/maps/place//data=!4m2!3m1!1s0x391904e4af9b1e35:0xee6f3c848c9e5341?utm_source=mstt_1
永远不会被加载。实际上,如果您只需要显示标记就足够了。但如果您需要坐标,则打开缩短的 URL 即可解决此问题https://maps.app.goo.gl/eEhh3
by HttpUrlConnection https://developer.android.com/reference/java/net/HttpURLConnection并通过以下方式获取重定向的“部分扩展”URLHttpUrlConnection.getURL() https://developer.android.com/reference/java/net/URLConnection#getURL() or via Location
连接打开后的标头字段和HttpUrlConnection.getInputStream() called https://developer.android.com/reference/java/net/HttpURLConnection#handling-network-sign-on。实际上,如果您加载来自的所有响应HttpUrlConnection
输入流为String
- 您可以通过以下方式在其中找到所需的纬度/经度坐标https://www.google.com/maps/preview/place
标签。就像是:
...
[[[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
。恕我直言,更好(但稍慢)的方法是传递从HttpUrlConnection.getURL() https://developer.android.com/reference/java/net/URLConnection#getURL() into 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);
}
}
}
and 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>
你可以得到这样的东西:
注意! Google 可以随时更改所描述的行为。