我遵循了本教程:http://android-er.blogspot.com/2013/03/embed-google-map-in-webview.html
我正在尝试在WebView中使用Google地图,但它无法获取我当前的位置。我在WebView上启用了JavaScript。我还需要启用什么?
有谁知道为什么会这样?它不应该促使我使用我当前的位置吗?
请注意,我对使用MapView作为替代方案不感兴趣。我想知道我需要在WebView上设置什么,或者可能在设备的位置服务上?
答案 0 :(得分:3)
您应该通过覆盖方法onGeolocationPermissionsShowPrompt
来允许网络视图访问您的位置:
webView.setWebChromeClient(new WebChromeClient(){
@Override
public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
callback.invoke(origin, true, false);
}
});
答案 1 :(得分:2)
在API 5.x及更低版本中,您需要
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
在AndroidManifest.xml
。
但要允许API 6.0+上的地理位置权限,您必须request the permission at runtime。
为此,请使用
private String mGeolocationOrigin;
private GeolocationPermissions.Callback mGeolocationCallback;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// other setup
myWebView.setWebChromeClient(new MyWebChromeClient());
}
private WebChromeClient mWebChromeClient = new WebChromeClient() {
@Override
public void onGeolocationPermissionsShowPrompt(String origin,
GeolocationPermissions.Callback callback) {
// Geolocation permissions coming from this app's Manifest will only be valid for devices with API_VERSION < 23.
// On API 23 and above, we must check for permission, and possibly ask for it.
final String permission = Manifest.permission.ACCESS_FINE_LOCATION;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M ||
ContextCompat.checkSelfPermission(MainActivity.this, permission) == PackageManager.PERMISSION_GRANTED) {
// we're on SDK < 23 OR user has already granted permission
callback.invoke(origin, true, false);
} else {
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, permission)) {
// user has denied this permission before and selected [/] DON'T ASK ME AGAIN
// TODO Best Practice: show an AlertDialog explaining why the user could allow this permission, then ask again
} else {
// ask the user for permissions
ActivityCompat.requestPermissions(MainActivity.this, new String[] {permission}, RP_ACCESS_LOCATION);
mGeolocationOrigin = origin;
mGeolocationCallback = callback;
}
}
}
}
并收到结果:
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case RP_ACCESS_LOCATION:
boolean allow = false;
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// user has allowed these permissions
allow = true;
}
if (mGeolocationCallback != null) {
mGeolocationCallback.invoke(mGeolocationOrigin, allow, false);
}
break;
}
}
在您的活动中。
答案 2 :(得分:1)
您可以使用Google地图试用GreenDroid。
答案 3 :(得分:0)
您必须启用android.permission.ACCESS_FINE_LOCATION
和android.permission.INTERNET
,
创建LocationManager
实例和LocationListener
实例
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new MyLocationListener();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10, locationListener);
并添加onLocationChanged(Location loc)
方法,其内部loc
生成经度和纬度(String long = loc.getLongitude(); String lat = loc.getLatitude();
)
现在使用long, lat
生成您的mapPath
字符串并继续生成WebView
您可以将其用于参考:http://www.rdcworld-android.blogspot.in/2012/01/get-current-location-coordinates-city.html