Not able to implement Google Map V2 - android

I'm trying to implement Google Map. But i'm getting Inflating error. Can anyone please provide a sample code to implement google map.
Thank you
Here is my code
public class GMap extends FragmentActivity implements LocationListener{
public static GoogleMap mMap;
LocationManager locationManager;
Criteria criteria;
String provider;
Location location;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gmap);
setUpMapIfNeeded();
}
#Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMapIfNeeded();
}
}
}

Related

setMyLocationEnabled is not working ( not localising me)

Am wroking with google map v2 on android and i set setMyLocationEnabled(true)
but there is no action or anything , it doesn't locate.
here is a part of my code , how can i fix it please :
public class Location extends Activity {
private GoogleMap mMap;
GoogleApiClient mGoogleApiClient;
Marker mMarker;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.location);
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
// mMap.moveCamera( CameraUpdateFactory.newLatLngZoom(new LatLng(36.755113, 3.058781) ,6) );
CameraUpdate center =
CameraUpdateFactory.newLatLng(new LatLng(36.755113,
3.058781));
CameraUpdate zoom = CameraUpdateFactory.zoomTo((float) 5.4);
mMap.moveCamera(center);
mMap.animateCamera(zoom);
mMap.setMyLocationEnabled(true);
));
}
public GoogleMap getMap() {
return mMap;
}
#Override
public void onStop() {
super.onStop();
if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
}

Insert Buttons and Menus (slide menu) in a FragmentActivity

I have an FragmentActivity which has a SupportMapFragment. I want to add buttons, menus (slide menu)...how could I do this?
here is my code:
public class MapsActivity extends FragmentActivity {
private GoogleMap mMap; // Might be null if Google Play services APK is not available.
private LocalizeDB db;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
setUpMapIfNeeded();
db = new LocalizeDB(this);
}
#Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}
FragmentActivity is a subclass of Activity. Most of all things you do with an Activity you can do with a FragmentActivity (some differences are stated here).
I think you will use your activity_maps layout to place additional buttons. The slide menu may be implemented as a Navigation Drawer. Let me know if I've missed something.
Hope it helps!

Mylocation button dont show up

I'm using Android Studio and using a map activity my map shows up fine, but the Mylocation button in the activity doesn't show. My google play services are installed. What am I doing wrong?
public class MapsActivity extends FragmentActivity {
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
setUpMapIfNeeded();
if (mMap != null) {
mMap.setMyLocationEnabled(true);
}
}
#Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
if (mMap == null) {
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
if (mMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
}
}
You have to add this line in your code, replace your old code for this one :
public class MapsActivity extends FragmentActivity {
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
setUpMapIfNeeded();
if (mMap != null) {
//edit this
}
}
#Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
if (mMap == null) {
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
mMap.setMyLocationEnabled(true);//here's the button
if (mMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
}
}
Also you have to add this in your manifest
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
If my answer didn't help you, you can follow this tutorial and do what it says step by step :)
Check this for your setUpMapIfNeeded() method:
Android Google Maps setMyLocationEnabled(true)
To enable the location on your Google Maps if the Google Maps is visible on the activity/fragment.
Create an instance of the Google Map:
GoogleMap googleMap;
Inside your main activity that implements LocationListener
Add this line in your code to see the My Location Button:
googleMap.setMyLocationEnabled(true);
Don't forget to add the permissions in the manifest file:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
For more details please refer to this tutorial.

Starting a map with current location

public class MainActivity extends FragmentActivity{
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
//mMap.setMyLocationEnabled(true);
}
}
This is my Activity.
I want to start my app with my current location. I looked for some codes but none have the answer that I want. Can someone tell me what I should add?
Did you try:
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 10);
map.animateCamera(cameraUpdate);
Please use Android's LocationManager to get Lat/Lng with respect to your current services. Please refer to this answer that explains the usage of LocationManager.
After getting a Location object from the LocationManager, use the lat/lng to position the map like this.

Using new OnMyLocationChangeListener in Google Maps Android API v2

Google finally added a callback for location changes in the Android API v2! However, I cannot intuitively get it to work, and Google does not have much documentation for it. Has anyone gotten it to work? What more do I need?
public class ... extends SupportMapFragment implements GoogleMap.OnMyLocationChangeListener {
GoogleMap map;
LocationManager locationManager;
String provider;
#Override
public void onActivityCreated(android.os.Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
map = getMap();
if (map != null) {
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
locationManager =(LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
provider = locationManager.getBestProvider(criteria, false);
}
}
#Override
public void onResume() {
super.onResume();
while(map == null) {
map = getMap();
map.setMyLocationEnabled(true);
map.setOnMyLocationChangeListener(this);
}
}
#Override
public void onMyLocationChange(Location loc) {
//implementation
}
}
This is how I do to navigate to the center of the map when we get the first location-update.
my class header:
public class FragActivity extends SherlockFragmentActivity implements OnMyLocationChangeListener
private GoogleMap mMap;
my mMap-setup:
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = customMapFragment.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null)
setUpMap();
}
setUpMap-method:
private void setUpMap() {
mMap.setMyLocationEnabled(true);
mMap.setOnMyLocationChangeListener(this);
}
and my onlocationchange:
#Override
public void onMyLocationChange(Location lastKnownLocation) {
CameraUpdate myLoc = CameraUpdateFactory.newCameraPosition(
new CameraPosition.Builder().target(new LatLng(lastKnownLocation.getLatitude(),
lastKnownLocation.getLongitude())).zoom(6).build());
mMap.moveCamera(myLoc);
mMap.setOnMyLocationChangeListener(null);
}
Works like a charm

Categories

Resources