Issue
I use location services in my app. If I send longitude and latitude to emulator via DDMS, blue dot animate to there. But if I close the app and restart again, blue dot doesn't seem. If you see blue dot, you have to send new longitude and latitude. I want to show blue dot every time when app start.
What can I do ?
onCreate:
mc = mapView.getController();
mapView.setBuiltInZoomControls(true);
mc.setZoom(15);
myLocOverlay = new MyLocationOverlay(this, mapView);
mapView.getOverlays().add(myLocOverlay);
myLocOverlay.runOnFirstFix(new Runnable() {
public void run() {
mc.animateTo(myLocOverlay.getMyLocation());
}
});
mapView.postInvalidate();
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
provider = lm.getBestProvider(criteria, false);
Location location = lm.getLastKnownLocation(provider);
if (location != null) {
onLocationChanged(location);
}
onLocationChanged(Location location)
String PK="";
double lat = location.getLatitude();
double lng = location.getLongitude();
Geocoder gc = new Geocoder(this,Locale.getDefault());
try{
List<Address> adresler = gc.getFromLocation(lat, lng, 1);
StringBuilder sb = new StringBuilder();
if(adresler.size()>0){
Address adres = adresler.get(0);
boolean sayi = false;
for(int i=0;i<adres.getMaxAddressLineIndex();i++){
if(i!=2){
sb.append(adres.getAddressLine(i)).append("\n");
}
else{
String full = adres.getAddressLine(i).toString();
char chars[] = full.toCharArray();
for(int k=0;k<chars.length;k++){
sayi = Character.isDigit(chars[k]);
if(sayi){
PK=PK+chars[k];
}
}
}
}
adresString = sb.toString();
new gpsYerBilgisiAS(PK).execute();
}
}
catch(IOException e){
}
}
onResume and onPause
@Override
protected void onResume() {
super.onResume();
lm.requestLocationUpdates(provider, 400, 1, this);
myLocOverlay.enableMyLocation();
}
@Override
protected void onPause() {
super.onPause();
lm.removeUpdates(this);
myLocOverlay.disableMyLocation();
}
Solution
In emulator, you have to send position each time you want to call onLocationChanged method. On device, it's automatically done if GPS is open. But i will only display "blue dot" when your device receives a location data via GPS or network.
Answered By - Serdar Samancıoğlu
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.