Reputation: 2938
I have made latitude and longtitude global variables.
It works fine in the MyLocationListener
class as it displays the current location latitude and longitude but when it loads url ( http://maps.google.com/maps?" + "saddr="
+ currentLattitude + "," + currentLongtitude
+ "&daddr=27.11,85.11")
) in webview, it loads the value 0 and 0.
Why?
public class ShowMapActivity extends Activity {
private WebView webView;
double currentLattitude;
double currentLongtitude;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
mlocListener);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("http://maps.google.com/maps?" + "saddr="
+ currentLattitude + "," + currentLongtitude
+ "&daddr=27.11,85.11");
}
public class MyLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location loc)
{
currentLattitude = loc.getLatitude();
currentLongtitude = loc.getLongitude();
Toast.makeText(
getApplicationContext(),
"Latitude is: " + currentLattitude + " Longitude is: "
+ currentLongtitude,
Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider)
{
Toast.makeText(getApplicationContext(),
"Gps Disabled",
Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderEnabled(String provider)
{
Toast.makeText(getApplicationContext(), "Gps Enabled",
Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
}
}
Upvotes: 1
Views: 1736
Reputation: 5378
Because you are opening the WebView in onCreate()
function and you get the location in onLocationChanged()
function. which it comes after, to solve this problem, if you want to always when location changed you update the web view, just put it inside the function onLocationChanged()
, if you want it to load once you can use boolean variable in your onLocationChanged()
to indicate that it is the first time you go in this function.
for efficiency: you better remove the updates after you get the location to avoid battery drain.
Upvotes: 1
Reputation: 15477
Your double variable is giving you the default values in below line.Because before updating the location below line is executing
webView.loadUrl("http://maps.google.com/maps?" + "saddr="
+ currentLattitude + "," + currentLongtitude
+ "&daddr=27.11,85.11");
Upvotes: 1
Reputation: 1146
Try to put this piece of code in onLocationChanged
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("http://maps.google.com/maps?" + "saddr="
+ currentLattitude + "," + currentLongtitude
+ "&daddr=27.11,85.11");
Upvotes: 3
Reputation: 40416
add this in onLocationChanged(...)
webView.loadUrl("http://maps.google.com/maps?" + "saddr="
+ currentLattitude + "," + currentLongtitude
+ "&daddr=27.11,85.11");
Upvotes: 1