Reputation: 13763
I am aware that there are lots of similar questions and I have read many of them but none worked for me. Partly because google maps sdk changed.
What I want to do is to show the user his current location on google maps.
Here is what I have done:
Added the following to dependencies in gradle file:
compile 'com.google.android.gms:play-services:7.5.0'
Added the following in the manifest file:
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
....
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="@string/google_maps_key" />
The onCreateView of my fragment:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mapFragment = (com.google.android.gms.maps.MapFragment)getChildFragmentManager().findFragmentById(R.id.map);
if (mapFragment == null){
fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
mapFragment = com.google.android.gms.maps.MapFragment.newInstance();
fragmentTransaction.replace(R.id.map, mapFragment).commit();
}
if (mapFragment != null){
initilizeMap();
}
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_map, container, false);
}
and the initializeMap
method:
private void initilizeMap(){
googleMap = mapFragment.getMap();
if (googleMap != null){
googleMap.getUiSettings().setAllGesturesEnabled(true);
googleMap.setMyLocationEnabled(true);
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
googleMap.getUiSettings().setCompassEnabled(true);
googleMap.setOnMyLocationChangeListener(myLocationChangeListener());
}
}
and here is the locationChangeListener
that was supposed to give me the current location:
private GoogleMap.OnMyLocationChangeListener myLocationChangeListener() {
return new GoogleMap.OnMyLocationChangeListener() {
@Override
public void onMyLocationChange(Location location) {
LatLng loc = new LatLng(location.getLatitude(), location.getLongitude());
CameraPosition cameraPosition = new CameraPosition.Builder().target(loc).zoom(15.0f).build();
CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition);
googleMap.moveCamera(cameraUpdate);
}
}
What happens is that the map appears but does not show the current location. It shows the whole world.
What am I missing or doing wrong? Thanks in advance.
Upvotes: 0
Views: 302
Reputation: 574
First you have to setup the Google Api client, to get the current location. you can view how to setup GoogleApiClient Here:
public class MainActivity extends AppCompatActivity implements `GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener, LocationListener {`
GoogleApiClient mGoogleApiClient;
Location mLastLocation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createLocationRequest();
TextView mTextView = (TextView)findViewById(R.id.mTextView);
mTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
buildGoogleApipClient();
mGoogleApiClient.connect();
}
});
}
protected synchronized void buildGoogleApipClient() {
mGoogleApiClient = new GoogleApiClient.Builder(MainActivity.this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
protected void onResume() {
super.onResume();
Toast.makeText(this, "Yes calling", Toast.LENGTH_LONG).show();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onConnected(Bundle bundle) {
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if(mLastLocation!=null){
Toast.makeText(this,mLastLocation.getLatitude()+"-----"+mLastLocation.getLongitude(),Toast.LENGTH_LONG).show();
}
if (true) {
startLocationUpdates();
}
}
Upvotes: 1