How to open Google Maps using a button on an Android app - android

I'm trying to make an app with a button called "Map" that will display a city map once pressed. So far I've made an app which contains the Map button and a separate app for my map which uses the Google Developers' tutorial to display a map when the app is opened. I'm having trouble when I try to combine both apps. I'm working on the OnClickListener but I don't know what I should put so that the map would be displayed after pressing the button.
EDIT
Below is my MapFragment class for the map:
public class MapFragment extends FragmentActivity
{
private GoogleMap mMap;
private LatLngBounds ILOILO = new LatLngBounds(new LatLng(10.6, 122.466), new LatLng(10.716, 122.566));
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.map_layout, container, false);
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
mMap.setMyLocationEnabled(true);
mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(ILOILO.getCenter(), 10));
if (mMap == null)
{
Toast.makeText(this, "Google Maps not available",
Toast.LENGTH_LONG).show();
}
return v;
}
}
Here's my XML file for the map:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="#+id/map"
map:uiCompass="false"
map:uiTiltGestures="true"
map:uiZoomControls="true"
map:uiZoomGestures="true"
map:uiRotateGestures="true"
map:uiScrollGestures="false"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
Here's for the button located in my MainActivity.java:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton mapButton = (ImageButton) findViewById(R.id.mapButton);
mapButton.setOnClickListener(mapListener);
}
private OnClickListener mapListener = new OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent(MainActivity, MapFragment.class);
startActivity(i);
}
};
I've also modified my Manifest since I've been getting "Unable to find explicit activity class (com.example.ipp.MapFragment); have you declared this activity in your AndroidManifest.xml?" error. Below is my manifest.
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="MapFragment" >
</activity>
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
PROBLEM
When I try to click the "Map" button, it displays my UI instead of displaying the map I've prepared.

Related

Android Google Map in Fragment always show the SupportMapFragment was null

I want build a android map application and Launch app will got null from SupportMapFragment(said my supportMapFragment is null), if you can point me where is my mistake, please help, many thanks.
code, XML and manifest as below.
By the way, the MapFragment is come from BottomNavigationView tab
R.id.navigation_map -> {
val mapFragment = MapFragment()
mapFragment.setArguments(intent.extras)
supportFragmentManager.beginTransaction()
.add(R.id.fragment_container, mapFragment).commit()
return#OnNavigationItemSelectedListener true
MapFragment.java
public class MapFragment extends Fragment implements OnMapReadyCallback {
private View rootView;
private AppCompatActivity context;
private SupportMapFragment supportMapFragment;
private GoogleMap map;
public MapFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
rootView = inflater.inflate(R.layout.fragment_map, container, false);
context = (AppCompatActivity) getActivity();
supportMapFragment = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.fragmentmap);
if (supportMapFragment != null) {
supportMapFragment.getMapAsync(this);
getFragmentManager().beginTransaction()
.replace(R.id.fragment_container, supportMapFragment).commit();
} else {
Toast.makeText(context, "Error - Map Fragment was null!!", Toast.LENGTH_SHORT).show();
}
return rootView;
}
#Override
public void onMapReady(GoogleMap googleMap) {
}
}
fragment_map.xml
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fragmentmap"
tools:context=".MainActivity"
android:name="com.google.android.gms.maps.SupportMapFragment" />
</android.support.design.widget.CoordinatorLayout>
Manifests
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<uses-library android:name="org.apache.http.legacy" android:required="false"/>
<activity
android:name=".MainActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="#string/google_maps_key" />
</application>
If you are using fragment inside another fragment then better to use getChildFragmentManager as it return a private FragmentManager for placing and managing Fragments inside of this Fragment.
supportMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.fragmentmap);
Reference: getChildFragmentManager

android app keeps crashing, trying to move from one activity to another

**** I found the sulotion *****
all I add to do is to move all my function from "protected void onCreate(Bundle savedInstanceState)"
and put them in "public static class PlaceholderFragment extends Fragment implements View.OnClickListener". Thank you all.
I really new to all of this and I just can't manage to move from one activity to another...
I don't see why it doesn't work and keeps crashing every time. Can anyone help?
here's MainActivity code:
public class MainActivity extends ActionBarActivity {
Button button1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener((OnClickListener) this);
}
private void buttonClick()
{
startActivity(new Intent("com.example.chaty.SignUpActivity"));
}
public void onClick(View v)
{
switch (v.getId())
{
case R.id.button1:
buttonClick();
break;
}
}
here is the Manifest:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.chaty.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.chaty.SignUpActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.chaty.SignUpActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
I am really new for all of this...
your app crashes, presumably for ClassCastException, because you are casting this to OnClickListener
button1.setOnClickListener((OnClickListener) this);
but your activity is not implementing the interface OnClickListener .
I would suggest you to change your button onClickListener like below.
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getBaseContext(), SignUpActivity.class));
}
});

how to use maps inside the fragment

i can show plain guide in inside part i have to add marker to specific geolocation
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="100dp"
android:name="com.google.android.gms.maps.SupportMapFragment"
/>
I don't did anything in java code however i extended my class with Fragment
Don't forget to add google-play-service-lib as a library project and
it should sharing the same workspace of your project
Java code
public class SomeFragment extends Fragment {
MapView mapView;
GoogleMap map;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.some_layout, container, false);
// Gets the MapView from the XML layout and creates it
mapView = (MapView) v.findViewById(R.id.mapview);
mapView.onCreate(savedInstanceState);
// Gets to GoogleMap from the MapView and does initialization stuff
map = mapView.getMap();
map.getUiSettings().setMyLocationButtonEnabled(false);
map.setMyLocationEnabled(true);
// Needs to call MapsInitializer before doing any CameraUpdateFactory calls
try {
MapsInitializer.initialize(this.getActivity());
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
// Updates the location and zoom of the MapView
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(43.1, -87.9), 10);
map.animateCamera(cameraUpdate);
return v;
}
#Override
public void onResume() {
mapView.onResume();
super.onResume();
}
#Override
public void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
#Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
}
Layout
<LinearLayout 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" >
<com.google.android.gms.maps.MapView android:id="#+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
Manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<permission
android:name="com.example.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.example.permission.MAPS_RECEIVE"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="your_key"/>
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<activity
android:name=".HomeActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Edit: After your edit new question arise.
After initializing map check if it is not null from official documentation
if(map != null)
{
Marker marker = map.addMarker(new MarkerOptions()
.position(new LatLng(37.7750, 122.4183))
.title("San Francisco")
.snippet("Population: 776733"));
}
In your layout, replace your code with this one :
<RelativeLayout
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="100dp">
</RelativeLayout>
and in your code :
private SupportMapFragment supportMapFragment;
private GoogleMap map;
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
FragmentManager fm = getChildFragmentManager();
supportMapFragment = (SupportMapFragment) fm.findFragmentById(R.id.map);
if (supportMapFragment == null) {
supportMapFragment = SupportMapFragment.newInstance();
fm.beginTransaction().replace(R.id.map, supportMapFragment).commit();
}
}
#Override
public void onResume() {
super.onResume();
...
map = supportMapFragment.getMap();
...
}

Android GoogleMap or SupportMapFragment - null pointer exception

In my app i showing google map version2 in a fragment. but i get Null pointer exception
at
mMap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
This is my full code:
public class SearchResultMap extends Fragment{
private GoogleMap mMap;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View fragmentView = inflater.inflate(R.layout.map, container, false);
mMap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); // line no : 28
mMap.addMarker(new MarkerOptions() .position(new LatLng(xxxxxx,xxxxxx)) .title("Current Location")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ball_pointer))
.snippet("xxxxx"));
return fragmentView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Manifest xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.fssd.spot"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<permission
android:name="com.fssd.spot.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-permission android:name="com.fssd.spot.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.fssd.spot.SplashScreen"
android:theme="#android:style/Theme.Light.NoTitleBar"
android:noHistory="true"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.fssd.spot.MainActivity"
android:screenOrientation="portrait"
android:theme="#style/tabTheme" >
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="xxxxxxxxxxxxxxxxxxxxxxxx" />
</application>
</manifest>
whole log cat:
FATAL EXCEPTION: main
java.lang.NullPointerException
at com.fssd.spot.search.SearchResultMap.onCreateView(SearchResultMap.java:28)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
at android.support.v4.app.Fragment.performStart(Fragment.java:1499)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:957)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:875)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:633)
at dalvik.system.NativeStart.main(Native Method)
None of these solutions fixed it for me -- but this answer from Mark Murphy (https://code.google.com/p/android/issues/detail?id=77714) did:
Use getChildFragmentManager() to access a nested fragment, not getFragmentManager(). http://developer.android.com/reference/android/support/v4/app/Fragment.html#getChildFragmentManager%28%29
i.e.:
public class MapFragment extends Fragment {
private GoogleMap map;
private SupportMapFragment mapFragment;
...
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup vg, Bundle data) {
View view = inflater.inflate(R.layout.map_view, vg, false);
FragmentManager fm = getChildFragmentManager();
mapFragment = (SupportMapFragment) fm.findFragmentById(R.id.map);
mapFragment.getMapAsync(new OnMapReadyCallback() {
public void onMapReady(GoogleMap googleMap) {
map = googleMap;
initMap();
}
});
}
}
try to change this:
FragmentManager myFM = getActivity().getSupportFragmentManager();
final SupportMapFragment myMAPF = (SupportMapFragment) myFM
.findFragmentById(R.id.map);
Try changing to
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
You're using getFragmentManager(), but casting it to SupportMapFragment
You also need to extend FragmentActivity: http://developer.android.com/reference/android/support/v4/app/FragmentActivity.html
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
if(map != null) {
mMap.addMarker(new MarkerOptions() .position(new LatLng(xxxxxx,xxxxxx)) .title("Current Location")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ball_pointer))
.snippet("xxxxx"));
}
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="xxxxxxxxxxxxxxxxxxxxxxxx" />
place your google api key here. You get from google Read the documentation.
After hours of search, I did in this way:
public class MapFragment extends Fragment {
private GoogleMap map;
SupportMapFragment mapFrag;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
setHasOptionsMenu(true);
View rootView = inflater.inflate(R.layout.fragment_map, container,
false);
fm = getActivity().getSupportFragmentManager();
map = ((SupportMapFragment) fm.findFragmentById(R.id.map)).getMap();
return rootView;
}
}
Try to add tools:context=".YourActivity" to the fragment xml:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".YourActivity" />
May be above answers work but what work for me is this.
{
SupportPlaceAutocompleteFragment supportPlaceAutocompleteFragmentSearch;
FragmentManager fragmentManagerSearch = getChildFragmentManager();
supportPlaceAutocompleteFragmentSearch =
(SupportPlaceAutocompleteFragment)
fragmentManagerSearch.findFragmentById(R.id.place_autocomplete_fragment_search);
}
if you developed your code with Kotlin, and want to add MapFragment to another Fragment you need to use SupportMapFragment but if you do it, you will get null exception so you should use childFragmentManager rather than supportFragmentManager and the code should be in onCreateView not in onCreate just add below code:
private lateinit var mapFragment : SupportMapFragment
mapFragment = childFragmentManager.findFragmentById(R.id.mapview) as SupportMapFragment
mapFragment.getMapAsync { googleMap ->
this.googleMap = googleMap
setGoogleMapSettings()
}

Android map not displaying

I want to display google map in Android ,i have map API even map is not showing and also not throwing any error.
I have use following
Java code
public class GoogleMapActivity extends MapActivity {
private Location myLocation;
protected MapView myMapView = null;
protected LocationManager myLocationManager = null;
protected MapController mapController;
List<Overlay> mapOverlays;
protected boolean isRouteDisplayed() {
return false;
}
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
this.myMapView = new MapView(this, "0jzIB6m5R1kLa_rGte-DS9PhF3KlSgqYHUZognA");
this.setContentView(myMapView);
myMapView.setBuiltInZoomControls(true);
myMapView.setSatellite(true);
mapController = myMapView.getController();
mapOverlays = myMapView.getOverlays();
this.myLocation = new Location("gps");
this.myLocation.setLongitude(77.52436144125092);
this.myLocation.setLatitude(13.05096452223662);
updateView();
}
private void updateView() {
Double lat = myLocation.getLatitude();
Double lng = myLocation.getLongitude();
GeoPoint point = new GeoPoint(lat.intValue(), lng.intValue());
mapController.setCenter(point);
}
}
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.googlemap"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".GoogleMapActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<uses-library android:name="com.google.android.maps" />
</application>
</manifest>
Finally i get output blank. please have a look at below screen
on please suggest me where i'm wrong
I think you have not added myMapView in current layout ...........
you created in java code
this.myMapView = new MapView(this,
"0jzIB6m5R1kLa_rGte-DS9PhF3KlSgqYHUZognA");
but looks not added in any layout.. as per link
If you dynamically create the MapView via its constructor, you can
supply the API key that way from Java code, but then you will need to
dynamically add it to your layout.
You cannot both have the widget in your layout and set the API key
in Java.
Solutions :
option 1 - Create a Linear layout in for XML and add the myMapView
in that.
option 2 -Create Map view in XML it self and get that by
findViewById.
Try placing the mapview on the layout xml with its api key there as well and then on the activity just get a reference of the map and continue normally.
main.xml with mapView:
<com.google.android.maps.MapView android:id="#+id/mapView"
android:layout_width="fill_parent" android:layout_height="fill_parent" android:enabled="true"
android:layout_centerInParent="true" android:clickable="true" android:apiKey="YOUR MAP KEY"/>
Activity:
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView)findViewById(R.id.mapView);
Below code which has working fine for me -
public class MapTabView extends MapActivity
{
MapView map;
String api = "02gY92RWvQgV-k5CUQvGPowJkw8HkN4Tmm-HDIQ";
#Override
protected void onCreate(Bundle icicle)
{
super.onCreate(icicle);
map = new MapView(this, api);
map.setClickable(true);
map.setEnabled(true);
setContentView(map);
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem menu_item)
{
switch (menu_item.getItemId()) {
case R.id.satelite:
map.setSatellite(true);
map.setTraffic(false);
break;
case R.id.traffic:
map.setTraffic(true);
map.setSatellite(false);
break;
default:
break;
}
return true;
}
}
Have a look at this TabMapsExample

Categories

Resources