我是谷歌地图的新手。只需点击此链接http://www.androidhive.info/2013/08/android-working-with-google-maps-v2/ 当我启动我的应用程序时,会出现以下错误:
Unfotunately Googlemapsv2 has stopped
我在stackoverflow上经历了很多帖子,但仍未解决问题所在。
logcat输出
01-14 18:05:22.301: E/AndroidRuntime(12239): FATAL EXCEPTION: main
01-14 18:05:22.301: E/AndroidRuntime(12239): java.lang.RuntimeException: Unable to start activity ComponentInfo{example.androidhive.googlemapsv2/example.androidhive.googlemapsv2.MainActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
01-14 18:05:22.301: E/AndroidRuntime(12239): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
01-14 18:05:22.301: E/AndroidRuntime(12239): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
01-14 18:05:22.301: E/AndroidRuntime(12239): at android.app.ActivityThread.access$600(ActivityThread.java:162)
01-14 18:05:22.301: E/AndroidRuntime(12239): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
01-14 18:05:22.301: E/AndroidRuntime(12239): at android.os.Handler.dispatchMessage(Handler.java:107)
01-14 18:05:22.301: E/AndroidRuntime(12239): at android.os.Looper.loop(Looper.java:194)
01-14 18:05:22.301: E/AndroidRuntime(12239): at android.app.ActivityThread.main(ActivityThread.java:5371)
01-14 18:05:22.301: E/AndroidRuntime(12239): at java.lang.reflect.Method.invokeNative(Native Method)
01-14 18:05:22.301: E/AndroidRuntime(12239): at java.lang.reflect.Method.invoke(Method.java:525)
01-14 18:05:22.301: E/AndroidRuntime(12239): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
01-14 18:05:22.301: E/AndroidRuntime(12239): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
01-14 18:05:22.301: E/AndroidRuntime(12239): at dalvik.system.NativeStart.main(Native Method)
01-14 18:05:22.301: E/AndroidRuntime(12239): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
01-14 18:05:22.301: E/AndroidRuntime(12239): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
01-14 18:05:22.301: E/AndroidRuntime(12239): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
01-14 18:05:22.301: E/AndroidRuntime(12239): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
01-14 18:05:22.301: E/AndroidRuntime(12239): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
01-14 18:05:22.301: E/AndroidRuntime(12239): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
01-14 18:05:22.301: E/AndroidRuntime(12239): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:281)
01-14 18:05:22.301: E/AndroidRuntime(12239): at android.app.Activity.setContentView(Activity.java:1881)
01-14 18:05:22.301: E/AndroidRuntime(12239): at example.androidhive.googlemapsv2.MainActivity.onCreate(MainActivity.java:18)
01-14 18:05:22.301: E/AndroidRuntime(12239): at android.app.Activity.performCreate(Activity.java:5122)
01-14 18:05:22.301: E/AndroidRuntime(12239): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
01-14 18:05:22.301: E/AndroidRuntime(12239): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
01-14 18:05:22.301: E/AndroidRuntime(12239): ... 11 more
01-14 18:05:22.301: E/AndroidRuntime(12239): Caused by: java.lang.RuntimeException: API key not found. Check that <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="your API key"/> is in the <application> element of AndroidManifest.xml
01-14 18:05:22.301: E/AndroidRuntime(12239): at maps.af.ay.a(Unknown Source)
01-14 18:05:22.301: E/AndroidRuntime(12239): at maps.af.ay.a(Unknown Source)
01-14 18:05:22.301: E/AndroidRuntime(12239): at maps.af.al.a(Unknown Source)
01-14 18:05:22.301: E/AndroidRuntime(12239): at maps.af.be.a(Unknown Source)
01-14 18:05:22.301: E/AndroidRuntime(12239): at maps.af.bd.a(Unknown Source)
01-14 18:05:22.301: E/AndroidRuntime(12239): at cmj.onTransact(SourceFile:107)
01-14 18:05:22.301: E/AndroidRuntime(12239): at android.os.Binder.transact(Binder.java:310)
01-14 18:05:22.301: E/AndroidRuntime(12239): at com.google.android.gms.maps.internal.IMapFragmentDelegate$a$a.onCreateView(Unknown Source)
01-14 18:05:22.301: E/AndroidRuntime(12239): at com.google.android.gms.maps.MapFragment$a.onCreateView(Unknown Source)
01-14 18:05:22.301: E/AndroidRuntime(12239): at com.google.android.gms.dynamic.a$4.b(Unknown Source)
01-14 18:05:22.301: E/AndroidRuntime(12239): at com.google.android.gms.dynamic.a.a(Unknown Source)
01-14 18:05:22.301: E/AndroidRuntime(12239): at com.google.android.gms.dynamic.a.onCreateView(Unknown Source)
01-14 18:05:22.301: E/AndroidRuntime(12239): at com.google.android.gms.maps.MapFragment.onCreateView(Unknown Source)
清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="example.androidhive.googlemapsv2"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="18" />
<permission
android:name="example.androidhive.googlemapsv2.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="example.androidhive.googlemapsv2.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<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.WRITE_EXTERNAL_STORAGE" />
<!-- Required to show current location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- Required OpenGL ES 2.0. for Maps V2 -->
<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="example.androidhive.googlemapsv2.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" />
</application>
</manifest>
MainActivity.java
package example.androidhive.googlemapsv2;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;
public class MainActivity extends Activity{
private GoogleMap googleMap;
private final String TAG ="GoogleMapsv2";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
initializeMap();
} catch (Exception e) {
e.printStackTrace();
}
}
// Function to load map. If map is not created it will create it for you
private void initializeMap() {
Log.d(TAG,"IntializeMap");
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
// Check if map is created successfully
if (googleMap == null) {
Toast.makeText(getApplicationContext(), "Unable to create maps",Toast.LENGTH_SHORT).show();
}
}
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
initializeMap();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
编辑: 当我在清单文件中进行以下更改时
<meta-data android:name="com.google.android.gms.version" android:value="AIzaSyBAjfcxZvbt_COHjy7igHZnLBfsO1cfoM8" />
出现另一个错误
01-14 18:28:21.323: W/Bundle(13254): Key com.google.android.gms.version expected Integer but value was a java.lang.String. The default value 0 was returned.
01-14 18:28:21.355: W/Bundle(13254): Attempt to cast generated internal exception:
01-14 18:28:21.355: W/Bundle(13254): java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
01-14 18:28:21.355: W/Bundle(13254): at android.os.Bundle.getInt(Bundle.java:945)
01-14 18:28:21.355: W/Bundle(13254): at android.os.Bundle.getInt(Bundle.java:927)
01-14 18:28:21.355: W/Bundle(13254): at com.google.android.gms.common.GooglePlayServicesUtil.n(Unknown Source)
01-14 18:28:21.355: W/Bundle(13254): at com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable(Unknown Source)
01-14 18:28:21.355: W/Bundle(13254): at com.google.android.gms.maps.internal.q.v(Unknown Source)
01-14 18:28:21.355: W/Bundle(13254): at com.google.android.gms.maps.internal.q.u(Unknown Source)
01-14 18:28:21.355: W/Bundle(13254): at com.google.android.gms.maps.MapsInitializer.initialize(Unknown Source)
01-14 18:28:21.355: W/Bundle(13254): at com.google.android.gms.maps.MapFragment$b.cE(Unknown Source)
01-14 18:28:21.355: W/Bundle(13254): at com.google.android.gms.maps.MapFragment$b.a(Unknown Source)
01-14 18:28:21.355: W/Bundle(13254): at com.google.android.gms.dynamic.a.a(Unknown Source)
01-14 18:28:21.355: W/Bundle(13254): at com.google.android.gms.dynamic.a.onInflate(Unknown Source)
01-14 18:28:21.355: W/Bundle(13254): at com.google.android.gms.maps.MapFragment.onInflate(Unknown Source)
01-14 18:28:21.355: W/Bundle(13254): at android.app.Activity.onCreateView(Activity.java:4734)
01-14 18:28:21.355: W/Bundle(13254): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
01-14 18:28:21.355: W/Bundle(13254): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
01-14 18:28:21.355: W/Bundle(13254): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
01-14 18:28:21.355: W/Bundle(13254): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
01-14 18:28:21.355: W/Bundle(13254): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
01-14 18:28:21.355: W/Bundle(13254): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:281)
01-14 18:28:21.355: W/Bundle(13254): at android.app.Activity.setContentView(Activity.java:1881)
01-14 18:28:21.355: W/Bundle(13254): at example.androidhive.googlemapsv2.MainActivity.onCreate(MainActivity.java:18)
01-14 18:28:21.355: W/Bundle(13254): at android.app.Activity.performCreate(Activity.java:5122)
01-14 18:28:21.355: W/Bundle(13254): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
01-14 18:28:21.355: W/Bundle(13254): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
01-14 18:28:21.355: W/Bundle(13254): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
01-14 18:28:21.355: W/Bundle(13254): at android.app.ActivityThread.access$600(ActivityThread.java:162)
01-14 18:28:21.355: W/Bundle(13254): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
01-14 18:28:21.355: W/Bundle(13254): at android.os.Handler.dispatchMessage(Handler.java:107)
01-14 18:28:21.355: W/Bundle(13254): at android.os.Looper.loop(Looper.java:194)
01-14 18:28:21.355: W/Bundle(13254): at android.app.ActivityThread.main(ActivityThread.java:5371)
01-14 18:28:21.355: W/Bundle(13254): at java.lang.reflect.Method.invokeNative(Native Method)
01-14 18:28:21.355: W/Bundle(13254): at java.lang.reflect.Method.invoke(Method.java:525)
01-14 18:28:21.355: W/Bundle(13254): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
01-14 18:28:21.355: W/Bundle(13254): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
01-14 18:28:21.355: W/Bundle(13254): at dalvik.system.NativeStart.main(Native Method)
01-14 18:28:21.374: D/AndroidRuntime(13254): Shutting down VM
01-14 18:28:21.374: W/dalvikvm(13254): threadid=1: thread exiting with uncaught exception (group=0x414929a8)
01-14 18:28:21.378: E/AndroidRuntime(13254): FATAL EXCEPTION: main
01-14 18:28:21.378: E/AndroidRuntime(13254): java.lang.RuntimeException: Unable to start activity ComponentInfo{example.androidhive.googlemapsv2/example.androidhive.googlemapsv2.MainActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
01-14 18:28:21.378: E/AndroidRuntime(13254): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
01-14 18:28:21.378: E/AndroidRuntime(13254): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
01-14 18:28:21.378: E/AndroidRuntime(13254): at android.app.ActivityThread.access$600(ActivityThread.java:162)
01-14 18:28:21.378: E/AndroidRuntime(13254): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
01-14 18:28:21.378: E/AndroidRuntime(13254): at android.os.Handler.dispatchMessage(Handler.java:107)
01-14 18:28:21.378: E/AndroidRuntime(13254): at android.os.Looper.loop(Looper.java:194)
01-14 18:28:21.378: E/AndroidRuntime(13254): at android.app.ActivityThread.main(ActivityThread.java:5371)
01-14 18:28:21.378: E/AndroidRuntime(13254): at java.lang.reflect.Method.invokeNative(Native Method)
01-14 18:28:21.378: E/AndroidRuntime(13254): at java.lang.reflect.Method.invoke(Method.java:525)
01-14 18:28:21.378: E/AndroidRuntime(13254): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
01-14 18:28:21.378: E/AndroidRuntime(13254): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
01-14 18:28:21.378: E/AndroidRuntime(13254): at dalvik.system.NativeStart.main(Native Method)
01-14 18:28:21.378: E/AndroidRuntime(13254): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
01-14 18:28:21.378: E/AndroidRuntime(13254): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
01-14 18:28:21.378: E/AndroidRuntime(13254): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
01-14 18:28:21.378: E/AndroidRuntime(13254): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
01-14 18:28:21.378: E/AndroidRuntime(13254): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
01-14 18:28:21.378: E/AndroidRuntime(13254): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
01-14 18:28:21.378: E/AndroidRuntime(13254): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:281)
01-14 18:28:21.378: E/AndroidRuntime(13254): at android.app.Activity.setContentView(Activity.java:1881)
01-14 18:28:21.378: E/AndroidRuntime(13254): at example.androidhive.googlemapsv2.MainActivity.onCreate(MainActivity.java:18)
01-14 18:28:21.378: E/AndroidRuntime(13254): at android.app.Activity.performCreate(Activity.java:5122)
01-14 18:28:21.378: E/AndroidRuntime(13254): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
01-14 18:28:21.378: E/AndroidRuntime(13254): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
01-14 18:28:21.378: E/AndroidRuntime(13254): ... 11 more
01-14 18:28:21.378: E/AndroidRuntime(13254): Caused by: java.lang.IllegalStateException: The meta-data tag in your app's AndroidManifest.xml does not have the right value. Expected 4030500 but found 0. You must have the following declaration within the <application> element: <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
请帮我找出解决方案。
答案 0 :(得分:2)
正如你对@gokhan回答的评论,你应该做
<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="@string/app_key" />
其中app_key
是您的Key
。
我建议您在ids.xml
文件夹中创建一个values
文件,其内容如下:
<resources>
<string name="app_id" translatable="false">1234567890</string>
<string name="app_key" translatable="false">QWERTTYruyfgREYT</string>
<string name="facebookAppId" translatable="false"></string>
答案 1 :(得分:0)
可能您忘记添加google_api_key,您必须添加密钥才能使用此服务
你应该添加你的密钥AndroidManifest.xml
<application>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="xxxxxxxxxx" />
</application>
像这样。
我希望这对你有帮助
答案 2 :(得分:0)
我认为你需要下载谷歌播放服务r10甚至r12。那你就不需要了
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
为了更好地理解,请按照link
进行操作答案 3 :(得分:0)
在我的清单文件中添加这两行之后就可以了。
对于eclipse,请添加:
<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="Your KEY" />
如果您使用的是带有gradle0.7 +的Android Studio,请添加以下行:
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="Your Key" />
答案 4 :(得分:0)
package com.synergyifs.libapp;
import android.Manifest;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AutoCompleteTextView;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.location.places.Place;
import com.google.android.gms.location.places.ui.PlacePicker;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.synergyifs.libapp.adapter.PlacesAutoCompleteAdapter;
import java.util.List;
import java.util.Locale;
/**
* Created by Admin34 on 23-03-2016.
*/
public class MapDemoActivity extends AppCompatActivity implements OnMapReadyCallback,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
GoogleMap.OnMarkerDragListener, GoogleMap.OnMapLongClickListener {
int PLACE_PICKER_REQUEST = 1;
private GoogleMap map;
private LatLng synergyIFS = new LatLng(18.511478, 73.801217);
private GoogleApiClient googleApiClient;
private double longitude;
private double latitude;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_activity);
SupportMapFragment fragment = (SupportMapFragment) this.getSupportFragmentManager().findFragmentById(R.id.main_map);
fragment.getMapAsync(this);
googleApiClient = new GoogleApiClient.Builder(MapDemoActivity.this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API).build();
}
private void initMap(GoogleMap googleMap) {
map = googleMap;
CameraUpdate center = CameraUpdateFactory.newLatLng(synergyIFS);
CameraUpdate zoom = CameraUpdateFactory.zoomTo(10f);
map.moveCamera(center);
map.animateCamera(zoom);
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
getCurrentLocation();
map.setMyLocationEnabled(true);
map.setOnMapLongClickListener(this);
map.setOnMarkerDragListener(this);
}
private void getCurrentLocation() {
Location location = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
if (location != null) {
//Getting longitude and latitude
longitude = location.getLongitude();
latitude = location.getLatitude();
//moving the map to location
moveMap();
}
}
private void moveMap() {
String msg = latitude + ", " + longitude;
//Creating a LatLng Object to store Coordinates
LatLng latLng = new LatLng(latitude, longitude);
//Adding marker to map
map.addMarker(new MarkerOptions()
.position(latLng) //setting position
.draggable(true) //Making the marker draggable
.title("Current Location")); //Adding a title
//Moving the camera
map.moveCamera(CameraUpdateFactory.newLatLng(latLng));
//Animating the camera
map.animateCamera(CameraUpdateFactory.zoomTo(15));
//Displaying current coordinates in toast
Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
}
protected void onStart() {
googleApiClient.connect();
super.onStart();
}
@Override
protected void onStop() {
googleApiClient.disconnect();
super.onStop();
}
@Override
public void onMapReady(GoogleMap googleMap) {
initMap(googleMap);
}
@Override
public void onConnected(Bundle bundle) {
getCurrentLocation();
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
@Override
public void onMarkerDragStart(Marker marker) {
}
@Override
public void onMarkerDrag(Marker marker) {
}
@Override
public void onMarkerDragEnd(Marker marker) {
latitude = marker.getPosition().latitude;
longitude = marker.getPosition().longitude;
//Moving the map
moveMap();
new ReverserGeoCoding(MapDemoActivity.this).execute(marker.getPosition());
}
@Override
public void onMapLongClick(LatLng latLng) {
map.clear();
//Adding a new marker to the current pressed position
map.addMarker(new MarkerOptions()
.position(latLng)
.draggable(true));
}
class ReverserGeoCoding extends AsyncTask<LatLng,Void,String>
{
Context mContext;
public ReverserGeoCoding(Context context){
super();
mContext = context;
}
@Override
protected String doInBackground(LatLng... params) {
Geocoder geocoder=new Geocoder(mContext);
double Lat=params[0].latitude;
double Long=params[0].longitude;
List<Address> addresses = null;
String addressText="";
try
{
addresses = geocoder.getFromLocation(Lat, Long,2);
}catch (Exception e)
{
e.printStackTrace();
}
if(addresses != null && addresses.size() > 0 ){
Address address = addresses.get(0);
addressText = String.format("%s, %s, %s",
address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",
address.getLocality(),
address.getCountryName());
}
return addressText;
}
@Override
protected void onPostExecute(String s) {
if(s!=null)
{
Toast.makeText(mContext,s,Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(mContext,"no address found",Toast.LENGTH_SHORT).show();
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest
package="com.synergyifs.libapp"
xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:largeHeap="true"
android:name=".app.MyApplication"
android:theme="@style/AppTheme">
<activity android:name=".SplashScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name=".HomeActivity"
android:windowSoftInputMode="adjustPan"
android:screenOrientation="portrait">
</activity>
<activity
android:name=".Login"
android:windowSoftInputMode="adjustPan"
android:screenOrientation="portrait">
</activity>
<activity
android:name=".RegisterUser"
android:windowSoftInputMode="adjustPan"
android:screenOrientation="portrait">
</activity>
<activity
android:name=".MapDemoActivity"
android:windowSoftInputMode="adjustPan"
android:screenOrientation="portrait">
</activity>
<activity
android:name=".PlacesActivity"
android:windowSoftInputMode="adjustPan"
android:screenOrientation="portrait">
</activity>
<!-- ATTENTION: This was auto-generated to add Google Play services to your project for
App Indexing. See https://g.co/AppIndexing/AndroidStudio for more information. -->
<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_api_key"/>
</application>
</manifest>