PDA

View Full Version : نمایش موقعیت فعلی کاربر در شروع اکتیویتی



sara.nikzadeh
دوشنبه 09 شهریور 1394, 11:55 صبح
با سلام خدمت دوستان گرامی
من تو برنامه ام از گوگل مپ استفاده کردم که در ادامه کدهاش رو میذارم.من میخوام تا اکتیویتی استارت میشه موقعیت فعلی کاربر رو نمایش بده و دوربین گوگل مپ به صورت خودکار به اون مکان انتفال پبدا کنه.
ممنون میشم کسی راهنمایی کنه


public class MainActivity extends ActionBarActivity implements
GooglePlayServicesClient.OnConnectionFailedListene r,
GooglePlayServicesClient.ConnectionCallbacks
{

private static final int GPS_ERRORDIALOG_REQUEST = 9001;

GoogleMap googleMap;
Marker marker;

private static final float Default_Zoom = 15;

LocationClient locationClient;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

if (serviceOk())
{
setContentView(R.layout.activity_map);
if (initMap())
{
Toast.makeText(this, "Ready to use", Toast.LENGTH_LONG).show();

locationClient = new LocationClient(this, this, this);
locationClient.connect();



}
}
else
{
Toast.makeText(this, "Ready not available", Toast.LENGTH_LONG).show();
}
}

public boolean serviceOk()
{
try
{
int isAvailable = GooglePlayServicesUtil.isGooglePlayServicesAvailab le(this);

if (isAvailable == ConnectionResult.SUCCESS)
{
return true;
}
else if (GooglePlayServicesUtil.isUserRecoverableError(isA vailable))
{
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable, this, GPS_ERRORDIALOG_REQUEST);
dialog.show();
}
else
{
Toast.makeText(this, "Cannot connect to Map", Toast.LENGTH_LONG).show();
}
}
catch (Exception ex)
{
Toast.makeText(this, ex.getMessage(), Toast.LENGTH_LONG).show();
}
return false;
}

private boolean initMap()
{
if (googleMap == null )
{
SupportMapFragment supportMapFragment =
(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id. map);
googleMap = supportMapFragment.getMap();
}
return (googleMap != null);
}

private void setMarker(String locality, String country, double lat, double lng)
{
if (marker != null)
{
marker.remove();
}

MarkerOptions markerOption = new MarkerOptions()
.title(locality)
.position(new LatLng(lat, lng))
.icon(BitmapDescriptorFactory.defaultMarker())
.draggable(true);
if (country.length() > 0)
{
markerOption.snippet(country);
}
marker = googleMap.addMarker(markerOption);
}

private void gotoCurrentLocation()
{
Location currentLocation = locationClient.getLastLocation();
if (currentLocation == null)
{
Toast.makeText(this, "Current location is not available", Toast.LENGTH_LONG).show();
}
else
{
LatLng ll = new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude());
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(ll, Default_Zoom);
googleMap.animateCamera(cameraUpdate);

setMarker("Current Location", "",
currentLocation.getLatitude(),
currentLocation.getLongitude());
}
}

@Override
public void onConnected(Bundle bundle)
{
Toast.makeText(this, "Connect", Toast.LENGTH_LONG).show();
}

@Override
public void onDisconnected()
{

}

@Override
public void onConnectionFailed(ConnectionResult arg0)
{

}


}

sara.nikzadeh
دوشنبه 09 شهریور 1394, 13:24 عصر
با تشکر از همگی مشکلم حل شد. راه حلش رو میگم واسه ارشیو. شاید یه روزی به درد دوستان تازه وارد بخوره.
داخل متد onConnected باید متد gotoCurrentLocation() را قراخوانی کرد.
با تشکر از همه دوستان