I have downloaded MapBox jar file from here and added to my eclipse project. I am getting following exception when running the app.
02-01 13:11:42.867: E/AndroidRuntime(2674):
java.lang.NoClassDefFoundError:
com.mapbox.mapboxsdk.MapboxAccountManager 02-01 13:11:42.867:
E/AndroidRuntime(2674): at
com.example.mapbox.MainActivity.onCreate(MainActivity.java:18)
here is my code:
public class MainActivity extends Activity {
private MapView mapView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MapboxAccountManager.start(this, getString(R.string.access_token));
setContentView(R.layout.activity_main);
mapView = (MapView) findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(MapboxMap arg0) {
// TODO Auto-generated method stub
}
});
}
activity_main
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:mapbox="http://schemas.android.com/apk/lib/mapbox"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".examples.basics.SimpleMapViewActivity">
<!-- Set the starting camera position and map style using xml-->
<com.mapbox.mapboxsdk.maps.MapView
android:id="#+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
mapbox:center_latitude="40.73581"
mapbox:center_longitude="-73.99155"
mapbox:style_url="#string/url"
mapbox:zoom="11" />
</RelativeLayout>
Please help me. I have searched a lot but nothing found useful. Thanks in advance.
Related
I am implementing Google maps in my android app. It works well but sometimes the app crashes and gives me this error: (Caused by: android.view.InflateException: Binary XML file line #2 in Error inflating class fragment).
I tried all solutions suggested but most of them are old and I still has the same issue.
Can you please help me with this issue while it still cause app crash.
this is the code used in Google Maps:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityTruckLocationBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
lat=lang="0";
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map2);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng myplace = new LatLng(29.2722744, 47.8585885);
//mMap.addMarker(new MarkerOptions().position(myplace).title("Marker in myplace"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(myplace,11));
mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
#Override
public void onMapLongClick(#NonNull LatLng latLng) {
lat=String.valueOf(latLng.latitude);
lang=String.valueOf(latLng.longitude);
redirect();
}
});
}
public void redirect(){
Intent intent=new Intent(TruckLocation.this,Truck_Profile.class);
intent.putExtra("intenttype","location");
intent.putExtra("lat",lat);
intent.putExtra("lang",lang);
startActivity(intent);
finish();
}
Here is the XML code:
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/map2"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TruckLocation" />
You have an issue in TruckLocationActivity.xml , you can share this xml with us to help you .
My application has 3 tabs and in one of them I want to display a map(HERE MAPS, not Google Maps). I was able to get the map to work perfectly in an Activity, but in Fragment Class when I try to cast the Fragment view to the MapFragment an error is thrown.
MapFragment mapFragment = (MapFragment)
getFragmentManager().findFragmentById(R.id.mapfragment);
Error:
Inconvetible types: cannot cast android.support.v4.app.support to com.here.android.mpa.mapping.MapFragment
Please correct me if Im wrong, but the reason for this to happen is because we canĀ“t use android.app.Fragment(used to display the map, as instructed in HERE MAPS DOC) in an android.support.v4.app.Fragment(used for the TabsLayout).
I found many questions related to this error using Google Maps. but just two(first,second)about the same error when using HERE MAPS, and none really helped to solve the problem.
In google Maps you can use SupportMapFragment(), but this mehod only works for Google Maps. Is there any solution for this using Here Maps? maybe a different approach to reach the same goal? or am I missing something when implementing this Here Maps in a TabLayout?
Any help will be appreciated!
My code:
MapFragment.java
public class Map extends Fragment {
public Map() {}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_map, container, false);
MapFragment mapFragment = (MapFragment)
getFragmentManager().findFragmentById(R.id.mapfragment);
mapFragment.init(new OnEngineInitListener() {
#Override
public void onEngineInitializationCompleted(
OnEngineInitListener.Error error) {
if (error == OnEngineInitListener.Error.NONE) {
com.here.android.mpa.mapping.Map map = mapFragment.getMap();
} else {
System.out.println("ERROR: Cannot initialize MapFragment");
}
}
};
return view;
}
fragment_map.xml
<FrameLayout 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"
tools:context="com.example.modulos.tabsMenu.Config">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="#+id/mapFragmentContainer"
android:layout_weight="1"
android:orientation="vertical"
android:gravity="center"
android:background="#aaa" >
<fragment
class="com.here.android.mpa.mapping.MapFragment"
android:id="#+id/mapfragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
android:gravity="center"
android:background="#aaa" >
<!-- other things here -->
</LinearLayout>
</FrameLayout>
Since the HERE Maps SDK only works with API Level 15 and above, there's no support for compatibility fragments (that you mainly need when you target lower API levels).
In some cases you still want to deal with support fragment even if you have Android 4 or newer as target, in those cases you have to use the the MapView of the HERE SDK and embedded it in your layout on your own.
See this code example where also MapView is used: https://tcs.ext.here.com/sdk_examples/MapDownloader.zip
So, it will look like this:
Add in your layout the MapView:
<com.here.android.mpa.mapping.MapView
android:id="#+id/ext_mapview"
android:visibility="visible"
android:layout_width="match_parent"
android:layout_height="match_parent" />
And then in your code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mapView = (MapView) findViewById(R.id.ext_mapview);
MapEngine.getInstance().init(this, engineInitHandler);
}
private OnEngineInitListener engineInitHandler = new OnEngineInitListener() {
#Override
public void onEngineInitializationCompleted(Error error) {
if (error == Error.NONE) {
map = new Map();
mapView.setMap(map);
// more map initial settings
} else {
Log.e(TAG, "ERROR: Cannot initialize MapEngine " + error);
}
}
};
#Override
public void onResume() {
super.onResume();
MapEngine.getInstance().onResume();
if (mapView != null) {
mapView.onResume();
}
}
#Override
public void onPause() {
if (mapView != null) {
mapView.onPause();
}
MapEngine.getInstance().onPause();
super.onPause();
}
It's important that you handle the MapEngine and MapView Pause and Resume, otherwise you would get poor performance results.
Surely is a silliness ask this, but do you check if are you using MapFragment from the right package?
You must be using this class com.here.android.mpa.mapping.MapFragment, maybe by error you import this one com.google.android.gms.maps.MapFragment.
I want to show a MapView in a fragment. The map is shown as what I expected, but it seems it is only shown as a picture. I cannot drag to move camera or spread/pinch to zoom. I also tried to use SupportMapFragment in my fragment, but got the same result.
I have a NavigationView in my Activity. I do not think there's conflict on the touch event of NavigationView and MapView.
Anyone met this before?
fragment
public class CustomizedMapFragment extends Fragment implements OnMapReadyCallback{
MapView mapView;
GoogleMap googleMap;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_map, container, false);
mapView = (MapView) rootView.findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);
return rootView;
}
#Override
public void onMapReady(GoogleMap googleMap) {
this.googleMap = googleMap;
this.googleMap.getUiSettings().setZoomControlsEnabled(true);
this.googleMap.getUiSettings().setZoomGesturesEnabled(true);
}
#Override
public void onResume() {
super.onResume();
mapView.onResume();
}
#Override
public void onPause() {
super.onPause();
mapView.onPause();
}
#Override
public void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
#Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
}
layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.google.android.gms.maps.MapView
android:layout_margin="16dp"
android:id="#+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
Try to use this UiSettings.setScrollGesturesEnabled(boolean), by using this you can scroll (pan) around the map by dragging the map with their finger.
Another way is configure these options when the map is created via XML attributes.
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
map:cameraBearing="112.5"
map:cameraTargetLat="-33.796923"
map:cameraTargetLng="150.922433"
map:cameraTilt="30"
map:cameraZoom="13"
map:mapType="normal"
map:uiCompass="false"
map:uiRotateGestures="true"
map:uiScrollGestures="false"
map:uiTiltGestures="true"
map:uiZoomControls="false"
map:uiZoomGestures="true"/>
But in order to use these custom attributes within your XML layout file, you must first add the following namespace declaration.
xmlns:map="http://schemas.android.com/apk/res-auto"
For more information, check this documentation.
I am trying to use Google Map V2. When I use map fragment .. Everything works.
However if I use mapview instead, then it displays blank activity.
What could be wrong?
Xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mapParent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.google.android.gms.maps.MapView
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
Main activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
You need to initialize the map yourself:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Gets the MapView from the XML layout and creates it
MapView mapView = (MapView) findViewById(R.id.map);
mapView.onCreate(savedInstanceState);
// Gets to GoogleMap from the MapView and does initialization stuff
GoogleMap map = mapView.getMap();
// Needs to call MapsInitializer before doing any CameraUpdateFactory calls
MapsInitializer.initialize(this);
mapView.onResume();
}
about MainActivity - If you only wanted to display simple map UI you can do following:
private GoogleMap map; // or private static GoogleMap map;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
[...]
try {
loadingObjectOfMainMap();
} catch (Exception e) {
e.printStackTrace(); // prints error to console/log
}
[...]
}
and then method:
protected void loadingObjectOfMainMap() {
if( map == null) {
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
map.setMyLocationEnabled(true); // will display blue dot at your location
}
}
About XML:
I highly recommed you to use fragments for google maps objects, like this:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:mapOptions="http://schemas.android.com/apk/res-auto"
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"
mapOptions:mapType="hybrid"/>
I recommend you simple tut, you will find all answers there
http://www.androidhive.info/2013/08/android-working-with-google-maps-v2/
(no, it's not my site - it's place where I started working with google maps api)
I want to use onTouch events (to handle the rotation gesture of an object on map). I cannot do it with extending the Activity in mainActivity and using fragments. So I guess I should use MapView class. How can I do this in new version of google maps android (V2)?
This will show nothing. a blank white screen on phone only.
The MainActivity:
public class MainActivity extends Activity {
// MapView mapV;
// GoogleMap map;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mapV = ((MapView) findViewById(R.id.map));
map = mapV.getMap();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
The activity_main.xml:
<RelativeLayout 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"
tools:context=".MainActivity" >
<com.google.android.gms.maps.MapView
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
In your activity you have to implement all the life cycle methods (onPause, etc) and in there forward it on to your map, e.g.
#Override
public void onPause() {
map.onPause();
super.onPause();
}