I'm really new to Android Development, and some days I'm trying to put a map in my application. I tried some things, but without success. I appreciate if someone can help me.
Things I've done:
I've tried several suggestions, but Google Maps APIs v2 Android doesn't work.
When running my project the following error appears:
06-15 20:31:37.666: E/AndroidRuntime(15687): FATAL EXCEPTION: main 06-15 20:31:37.666: E/AndroidRuntime(15687): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hellomap/com.example.hellomap.MainActivity}: android.view.InflateException: Binary XML file line #3: Error inflating class fragment 06-15 20:31:37.666: E/AndroidRuntime(15687): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2092) 06-15 20:31:37.666: E/AndroidRuntime(15687): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117) 06-15 20:31:37.666: E/AndroidRuntime(15687): at android.app.ActivityThread.access$700(ActivityThread.java:134) 06-15 20:31:37.666: E/AndroidRuntime(15687): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218) 06-15 20:31:37.666: E/AndroidRuntime(15687): at android.os.Handler.dispatchMessage(Handler.java:99) 06-15 20:31:37.666: E/AndroidRuntime(15687): at android.os.Looper.loop(Looper.java:137) 06-15 20:31:37.666: E/AndroidRuntime(15687): at android.app.ActivityThread.main(ActivityThread.java:4867) 06-15 20:31:37.666: E/AndroidRuntime(15687): at java.lang.reflect.Method.invokeNative(Native Method) 06-15 20:31:37.666: E/AndroidRuntime(15687): at java.lang.reflect.Method.invoke(Method.java:511) 06-15 20:31:37.666: E/AndroidRuntime(15687): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007) 06-15 20:31:37.666: E/AndroidRuntime(15687): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774) 06-15 20:31:37.666: E/AndroidRuntime(15687): at dalvik.system.NativeStart.main(Native Method) 06-15 20:31:37.666: E/AndroidRuntime(15687): Caused by: android.view.InflateException: Binary XML file line #3: Error inflating class fragment 06-15 20:31:37.666: E/AndroidRuntime(15687): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 06-15 20:31:37.666: E/AndroidRuntime(15687): at android.view.LayoutInflater.inflate(LayoutInflater.java:466) 06-15 20:31:37.666: E/AndroidRuntime(15687): at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 06-15 20:31:37.666: E/AndroidRuntime(15687): at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 06-15 20:31:37.666: E/AndroidRuntime(15687): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:316) 06-15 20:31:37.666: E/AndroidRuntime(15687): at android.app.Activity.setContentView(Activity.java:1901) 06-15 20:31:37.666: E/AndroidRuntime(15687): at com.example.hellomap.MainActivity.onCreate(MainActivity.java:14) 06-15 20:31:37.666: E/AndroidRuntime(15687): at android.app.Activity.performCreate(Activity.java:5047) 06-15 20:31:37.666: E/AndroidRuntime(15687): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094) 06-15 20:31:37.666: E/AndroidRuntime(15687): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2056) 06-15 20:31:37.666: E/AndroidRuntime(15687): ... 11 more 06-15 20:31:37.666: E/AndroidRuntime(15687): Caused by: java.lang.IllegalStateException: The meta-data tag in your app's AndroidManifest.xml does not have the right value. Expected 4323000 but found 0. You must have the following declaration within the element: 06-15 20:31:37.666: E/AndroidRuntime(15687): at com.google.android.gms.common.GooglePlayServicesUtil.t(Unknown Source) 06-15 20:31:37.666: E/AndroidRuntime(15687): at com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable(Unknown Source) 06-15 20:31:37.666: E/AndroidRuntime(15687): at com.google.android.gms.maps.internal.q.B(Unknown Source) 06-15 20:31:37.666: E/AndroidRuntime(15687): at com.google.android.gms.maps.internal.q.A(Unknown Source) 06-15 20:31:37.666: E/AndroidRuntime(15687): at com.google.android.gms.maps.MapsInitializer.initialize(Unknown Source) 06-15 20:31:37.666: E/AndroidRuntime(15687): at com.google.android.gms.maps.SupportMapFragment$b.gW(Unknown Source) 06-15 20:31:37.666: E/AndroidRuntime(15687): at com.google.android.gms.maps.SupportMapFragment$b.a(Unknown Source) 06-15 20:31:37.666: E/AndroidRuntime(15687): at com.google.android.gms.dynamic.a.a(Unknown Source) 06-15 20:31:37.666: E/AndroidRuntime(15687): at com.google.android.gms.dynamic.a.onInflate(Unknown Source) 06-15 20:31:37.666: E/AndroidRuntime(15687): at com.google.android.gms.maps.SupportMapFragment.onInflate(Unknown Source) 06-15 20:31:37.666: E/AndroidRuntime(15687): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:290) 06-15 20:31:37.666: E/AndroidRuntime(15687): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676) 06-15 20:31:37.666: E/AndroidRuntime(15687): ... 20 more
Here is main.xml file
<?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" android:id="@+id/map" android:name="com.google.android.gms.maps.SupportMapFragment" android:layout_width="match_parent" android:layout_height="match_parent" map:mapType="normal"/>
Here is manifest file
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.hellomap" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17"/> <!-- The following four permissions --> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <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-feature android:glEsVersion="0x00020000" android:required="true"/> <application android:label="@string/app_name" android:icon="@drawable/ic_launcher"> <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="AIzaSyDD0f1pf-64LHNlTUWl6a5qeoWqqMw0e3s"/> <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> </application> </manifest>
and finally java
package com.example.hellomap; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.SupportMapFragment; public class MainActivity extends FragmentActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } }
Tank's
Try my other post. I have explained the working of maps in detail.
You should be able to use maps now.
Want to use GoogleMaps - OnMyLocationChangeListener but can't implement it? Any other options
Here's the layout snippet for maps I am using :
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="250dp"
class="com.google.android.gms.maps.SupportMapFragment" />
The permission are like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mike.mylocation"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<permission
android:name="com.mike.maps.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.mapsexample.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" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.mike.mylocation.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>
<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="YOUE API WILL GO HERE" />
</application>
</manifest>
Also Don't forget to use these callbacks or your battery will be drained..:p
@Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
locationManager.removeUpdates(this);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
1, this);
}
EDIT:
If Having issues with the previous answer, here's how the code goes:
1) In your MainActivity, extend the Fragment Activiy and implement LocationListener, OnMapClickListener, OnMapLongClickListener like this:
public class MainActivity extends FragmentActivity implements LocationListener,
OnMapClickListener, OnMapLongClickListener {
private static final int GPS_ERRORDIALOG_REQUEST = 9001;
GoogleMap map;
List<Address> matches;
String addressText;
String addressLine;
double latitude;
double longitude;
AppUtils mAppUtils;
Context context;
LocationManager locationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this;
if (servicesOK()) {
// Toast.makeText(this, "Ready to map!!", Toast.LENGTH_LONG).show();
setContentView(R.layout.activity_main);
// Getting reference to the SupportMapFragment of activity_main.xml
SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
// Getting GoogleMap object from the fragment
map = fm.getMap();
// Enabling MyLocation Layer of Google Map
map.setMyLocationEnabled(true);
// Getting LocationManager object from System Service
// LOCATION_SERVICE
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
// Creating a criteria object to retrieve provider
Criteria criteria = new Criteria();
// Getting the name of the best provider
String provider = locationManager.getBestProvider(criteria, true);
// Getting Current Location
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
onLocationChanged(location);
}
locationManager.requestLocationUpdates(provider, 20000, 0, this);
} else {
setContentView(R.layout.activity_main);
}
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
//This is the main stuff here. This is when the location is changed.
@Override
public void onLocationChanged(Location location) {
// Getting latitude of the current location
latitude = location.getLatitude();
// Getting longitude of the current location
longitude = location.getLongitude();
// Creating a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
// Showing the current location in Google Map
map.moveCamera(CameraUpdateFactory.newLatLng(latLng));
// Zoom in the Google Map
map.animateCamera(CameraUpdateFactory.zoomTo(12));
map.addMarker(
new MarkerOptions().position(new LatLng(latitude, longitude))
.title("My Location" + "\n")).showInfoWindow();
map.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
marker.showInfoWindow();
return false;
}
});
//This is optional........................................................
Geocoder geoCoder = new Geocoder(this);
try {
matches = geoCoder.getFromLocation(latitude, longitude, 1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Address bestMatch = (matches.isEmpty() ? null : matches.get(0));
addressText = String.format("%s, %s, %s", bestMatch
.getMaxAddressLineIndex() > 0 ? bestMatch.getAddressLine(0)
: "", bestMatch.getLocality(), bestMatch.getCountryName());
//...........................................................................
}
@Override
public void onMapLongClick(LatLng point) {
// TODO Auto-generated method stub
}
@Override
public void onMapClick(LatLng point) {
// TODO Auto-generated method stub
}
//Check if the service is OK
public boolean servicesOK() {
int isAvailable = GooglePlayServicesUtil
.isGooglePlayServicesAvailable(this);
if (isAvailable == ConnectionResult.SUCCESS) {
return true;
} else if (GooglePlayServicesUtil.isUserRecoverableError(isAvailable)) {
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable,
this, GPS_ERRORDIALOG_REQUEST);
dialog.show();
} else {
Toast.makeText(this, "Cant connect!!", Toast.LENGTH_SHORT).show();
}
return false;
}
@Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
locationManager.removeUpdates(this);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
1, this);
}
2) Layout is the same as mentioned above.
Hope this help.. :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.