I have attempted the following activity using Google Maps API v2 (Android). The API Key generated is working and the necessary permissions in the manifest.xml has been declared accordingly.
Here is the code for the activity class, MapView.java, which references the xml Layout Google_Map.xml:
**Map View.java**
public class MapView extends Activity implements LocationListener {
private GoogleMap googleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_google_map);
int status= GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());
if(status!=ConnectionResult.SUCCESS){
int reqCode=10;
Dialog diag= GooglePlayServicesUtil.getErrorDialog(status,this,reqCode);
}
else{
buildMap();
}
}
private void buildMap(){
if(googleMap==null){
googleMap= ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
googleMap.setMyLocationEnabled(true);
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
LocationManager locManager= (LocationManager)getSystemService(LOCATION_SERVICE);
Criteria criteria= new Criteria();
String provider= locManager.getBestProvider(criteria,true);
Location location= locManager.getLastKnownLocation(provider);
if(googleMap==null){
Toast.makeText(this,"Unable to initialise map at the moment...",Toast.LENGTH_SHORT).show();
}
if(location!=null){
onLocationChanged(location);
}
locManager.requestLocationUpdates(provider,20000,0,this);
}
}
@Override
public void onLocationChanged(Location loc){
double lat= loc.getLatitude();
double lng=loc.getLongitude();
LatLng latlng= new LatLng(lat,lng);
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latlng));
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
}
@Override
public void onProviderDisabled(String provider){}
@Override
public void onProviderEnabled(String provider){}
@Override
public void onStatusChanged(String provider,int status, Bundle extra){}
@Override
protected void onResume(){
super.onResume();
buildMap();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.google_map, menu);
return true;
}
@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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
layout file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.hotel.moeccefamilytime.MapView">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"/>
</RelativeLayout>
My experience with Android is very basic and this is my first project to incorporate Google Maps to provide geographical capabilities for users in obtaining directions to specific places.
This was the error which had occurred while debugging; the camera does not zoom in on the current location. (Location settings have been activated on the phone)
Here are the following references which have been provided but the solutions are not working for my case:
- Could not find class 'com.google.android.gms.location.internal.ParcelableGeofence', referenced from method glt.a
- Could not find class 'gpr', referenced from method gps.a
- Could not find class 'com.google.android.gms.location.internal.ParcelableGeofence referenced from method glt.a
Thank you so much if you could point me in the right direction.