onMapReady method not applying to Google Maps Display - android

I am looking to display a Google Maps Display on a fragment layout within android studio. I have already downloaded Google play services in the SDK and have made the appropriate changes to my gradle files (I Think). I have made some changes to the android manifest file as well to grant user permissions etc. At run time a google maps display renders but the lng and lat are set to 0 so when the map displays, it shows me an area around Africa. I have made methods in the java file such as onMapReady, which is supposed to display the map at a different location and have it more zoomed in. The onMapReady function calls another method called goToLocationZoom, which holds the required functionality to allow the map to move to this location but for some reason everytime I run it, it always stays to the area around Africa, even though I have changed the lng and lat values. I am following a tutorial to achieve this, and in there Activty_home.XML file, they have their layout to linear, whilst mine is set to constraint but I don't think this would cause the issue. Does anyone know how to display the map to the lng and lat values that I have specified in the java file?
Below is my Java file code:
com.example.cmpkbest.lifestylefitnessapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.LatLng;
public class ViewLifestyles extends AppCompatActivity implements
GoogleMap mGoogleMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_lifestyles);
initMap();
}
private void initMap () {
MapFragment mapFragment = (MapFragment) getFragmentManager ().findFragmentById(R.id.mapFragment);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;
goToLocationZoom (53.408743, -2.984154, 15);
}
private void goToLocation (double lat, double lng) {
LatLng ll = new LatLng(lat, lng);
CameraUpdate update = CameraUpdateFactory.newLatLng(ll);
mGoogleMap.moveCamera(update);
}
private void goToLocationZoom (double lat, double lng, float zoom) {
LatLng ll = new LatLng(lat, lng);
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(ll, zoom);
mGoogleMap.moveCamera(update);
}
}
Below is my Manifest code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.cmpkbest.lifestylefitnessapp">
<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" />
<permission android:name="com.example.cmpkbest.lifestylefitnessapp.permission.MAPS_RECIEVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.cmpkbest.lifestylefitnessapp.permission.MAPS_RECIEVE" />
<uses-permission android:name="com.google.android.providers.gsf.permissions.READ_GSERVICES" />
<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" >
<activity android:name=".RegistrationPage">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Login" />
<activity android:name=".Home" />
<activity android:name=".ViewLifestyles" />
<activity android:name=".Workouts" />
<activity android:name=".Notemaker"></activity>
<meta-data android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyAkAQlA2B8_E5pPNH_jdAWSJpf915uozi8 " />
<meta-data android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
</application>
</manifest>
Below is my Gradle Files:
apply plugin: 'com.android.application'
repositories {
maven {
url 'https://maven.google.com'
}
}
android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
defaultConfig {
applicationId "com.example.cmpkbest.lifestylefitnessapp"
minSdkVersion 23
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:+'
testCompile 'junit:junit:4.12'
compile 'com.google.android.gms:play-services-maps:11.0.4'
}
Finally, my Layout.XML file
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg5"
tools:context="com.example.cmpkbest.lifestylefitnessapp.ViewLifestyles"
tools:layout_editor_absoluteY="81dp"
tools:layout_editor_absoluteX="0dp">
<fragment
android:id="#+id/mapFragment"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="346dp"
android:layout_height="212dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintVertical_bias="0.084" />
</android.support.constraint.ConstraintLayout>

I also had a project doing same output. I handle it this way.
So first you have to initialize the map
if (mMap == null) {
mMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMap();
// check if map is created successfully or not
if (mMap == null) {
Toast.makeText(getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
Then i call setUpMap right after initializing it, not relying on onMapReady
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(
lastLocation.getLatitude(), lastLocation.getLongitude()), 15));
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
mMap.setIndoorEnabled(true);
mMap.setMyLocationEnabled(true);
Hope this helps

Related

How to start Google Map activity, with Intent

I have created activity: MapsActivity, which shows my location on map.
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private LocationManager locationManager;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false));
double lat = location.getLatitude();
double lng = location.getLongitude();
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
mMap.setMyLocationEnabled(true);
// Add a marker in Sydney and move the camera
LatLng place = new LatLng(lat,lng);
mMap.addMarker(new MarkerOptions().position(place).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(place));
}
}
I created MainActivity, with a button, which I want to click to move to MapsActivity, here it's code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void showMyLocation(View view){
Intent intentControls = new Intent(this, MapsActivity.class);
startActivity(intentControls);
}
}
Gradle
android {
compileSdkVersion 27
buildToolsVersion "27.0.1"
defaultConfig {
applicationId "com.example.lukasz.aaaaa"
minSdkVersion 21
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:26.0.0-alpha1'
compile 'com.google.android.gms:play-services-maps:10.2.6'
compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha8'
testCompile 'junit:junit:4.12'
}
AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.lukasz.aaaaa">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<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-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="#string/google_maps_key" />
<activity
android:name=".MapsActivity"
android:label="#string/title_activity_maps">
</activity>
<activity android:name=".MainActivity"
android:label="Mapy">>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
The problem is, that after I click the button in my MainActivity, the application crashes with
FATAL EXCEPTION: main
Process: com.example.lukasz.aaaaa, PID: 2433
java.lang.IllegalArgumentException: invalid provider: null
at android.location.LocationManager.checkProvider(LocationManager.java:2099)
at android.location.LocationManager.getLastKnownLocation(LocationManager.java:1200)
at com.example.lukasz.aaaaa.MapsActivity.onMapReady(MapsActivity.java:56)
at com.google.android.gms.maps.SupportMapFragment$zza$1.zza(Unknown Source:7)
at com.google.android.gms.maps.internal.zzt$zza.onTransact(Unknown Source:30)
at android.os.Binder.transact(Binder.java:627)
at gl.b(:com.google.android.gms.DynamiteModulesB#11577470:20)
at com.google.android.gms.maps.internal.bf.a(:com.google.android.gms.DynamiteModulesB#11577470:5)
at com.google.maps.api.android.lib6.impl.bc.run(:com.google.android.gms.DynamiteModulesB#11577470:5)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)r
My question is how do I run Google Map Activity from another activity, using Intent?
keep below permission above application Tag
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<application
......
.....
<activity>
.....
</activity>
</application>
(or)
You are getting this error most probably, due to un-availability of
GPS Providers. Please ask the person to check settings if Gps
Provider or Network Provider has been enabled into Location
settings.

Adding Google Maps - Only seeing blank screen

I'm trying to add Google Maps. However, after building the apk from Build > Generate Signed apk and installing it to a real device, the activity only shows a blank screen, with the Google logo in the lower left. Is there something wrong with the following codes?
Notes
API Key is in both google_maps_api.xml and strings.xml, and API Key in below code is not shown
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.thesis.cutaneous.testapp1">
<permission
android:name="com.thesis.cutaneous.testapp1.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-permission android:name="com.thesis.cutaneous.testapp1.permission.MAPS_RECEIVE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MapsActivity"
android:label="#string/title_activity_maps">
<intent-filter>
<action android:name="com.thesis.cutaneous.testapp1.MapsActivity" />
<category android:name="android.intent.category.DEFAULT" />
</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>
</manifest>
Gradle (Module)
apply plugin: 'com.android.application'
android {
compileSdkVersion 24
buildToolsVersion '24.0.1'
defaultConfig {
applicationId "com.thesis.cutaneous.testapp1"
minSdkVersion 16
targetSdkVersion 24
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.2.0'
compile 'com.google.android.gms:play-services-maps:9.4.0'
}
Gradle (Project)
buildscript {
repositories {
maven { url 'http://repo1.maven.org/maven2' }
jcenter { url 'http://jcenter.bintray.com/' }
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.3'
}
}
allprojects {
repositories {
maven { url 'http://repo1.maven.org/maven2' }
jcenter { url 'http://jcenter.bintray.com/' }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
MainActivity.java
package com.thesis.cutaneous.testapp1;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private static Button btn1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) findViewById(R.id.button);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent("com.thesis.cutaneous.testapp1.MapsActivity"));
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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="com.thesis.cutaneous.testapp1.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="#+id/textView" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true" />
</RelativeLayout>
MapsActivity.java
package com.thesis.cutaneous.testapp1;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
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.MarkerOptions;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng Angeles = new LatLng(15.1427956, 120.5977875);
mMap.addMarker(new MarkerOptions().position(Angeles).title("Marker in Angeles"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(Angeles));
}
}
activity_maps.xml
<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/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.thesis.cutaneous.testapp1.MapsActivity" />
Blank map is caused by incorrect google_maps_key. Note that there are 2 different keys - one for debug, one for release build. You have to register both on https://console.developers.google.com/. To determine your release key run something like this in your project directory:
"c:\Program Files\Java\jdk1.8.0_20\bin"\keytool -list -v -keystore keystore.jks

setOnMyLocationChangeListener and setMyLocationEnabled arent work on map

I am doing what is written in this link but it isn't working(image1)(line 46, 47 and 53) https://io2015codelabs.appspot.com/codelabs/fire-place#7
for image ->
MapsActiviy.java
import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.content.ContextCompat;
import android.view.ViewTreeObserver;
import android.widget.Button;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMyLocationChangeListener;
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.LatLngBounds;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private LatLngBounds.Builder mBounds = new LatLngBounds.Builder();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Set up Google Maps
SupportMapFragment mapFragment = (SupportMapFragment)
getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
/**
* Map setup. This is called when the GoogleMap is available to manipulate.
*/
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Pad the map controls to make room for the button - note that the button may not have
// been laid out yet.
final Button button = (Button) findViewById(R.id.checkout_button);
button.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
mMap.setPadding(0, button.getHeight(), 0, 0);
}
}
);
mMap.setMyLocationEnabled(true);
mMap.setOnMyLocationChangeListener(new OnMyLocationChangeListener() {
#Override
public void onMyLocationChange(Location location) {
LatLng ll = new LatLng(location.getLatitude(), location.getLongitude());
addPointToViewPort(ll);
// we only want to grab the location once, to allow the user to pan and zoom freely.
mMap.setOnMyLocationChangeListener(null);
}
});
}
private void addPointToViewPort(LatLng newPoint) {
mBounds.include(newPoint);
mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(mBounds.build(),
findViewById(R.id.checkout_button).getHeight()));
}
}
activity_maps.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<Button android:id="#+id/checkout_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/check_out"
android:onClick="checkOut"/>
<fragment 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:id="#+id/map"
tools:context=".MapsActivity"
android:name="com.google.android.gms.maps.SupportMapFragment"/>
</FrameLayout>
build.gradle(Module: App)
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion '23.0.3'
defaultConfig {
applicationId "com.sahikaaylin.checkout"
minSdkVersion 19
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors {
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.google.android.gms:play-services:9.0.1'
compile 'com.firebase:firebase-client-android:2.3.1'
compile 'com.android.support:appcompat-v7:23.4.0'
}
for manifest.xml
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme" >
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="#string/google_maps_key" />
<activity
android:name=".MapsActivity"
android:label="#string/title_activity_maps" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
As seen on the Android docs : https://developers.google.com/android/reference/com/google/android/gms/maps/GoogleMap.OnMyLocationChangeListener
GoogleMap.OnMyLocationChangeListener is now deprecated. The tutorial you are using is simply already too old.
From the link above, you will be able to be redirected to newer tutorials and how-to guides (which will also solve the setMyLocationEnabled error).
EDIT : What I recommended is now deprecated, such is life in software development. If you're looking for the newest way of doing it, check out the Android documentations.

Show location on map from address

I want to show user location as per address submitted by him. For that i am using Geocoder. But i got following error.
10-17 18:21:02.734 1914-1934/com.example.googlemapdemo E/GMPM: getGoogleAppId failed with status: 10
10-17 18:21:02.735 1914-1934/com.example.googlemapdemo E/GMPM: Uploading is not possible. App measurement disabled
I have get google api key by following all steps. Here is my code
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.google.android.gms:play-services:8.1.0'
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.android.support:design:23.0.1'
}
MainActivity.java
Geocoder geocoder = new Geocoder(getApplicationContext());
String result=null;
try {
List addressList = geocoder.getFromLocationName(address.getText().toString(),1);
if(addressList!=null && addressList.size() > 0) {
Address address = (Address) addressList.get(0);
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria,true);
double lat = address.getLatitude();
double longti = address.getLongitude();
Log.e("lat..long","lat...long"+lat+"....."+longti);
Intent intent = new Intent(MainActivity.this, MapsActivity.class);
intent.putExtra("Lat",lat);
intent.putExtra("Long",longti);
startActivity(intent);
}
} catch (IOException e) {
e.printStackTrace();
}
map.xml
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/map"
tools:context="com.example.googlemapdemo.MapsActivity"
android:name="com.google.android.gms.maps.SupportMapFragment"/>
MapActivity
package com.example.googlemapdemo;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
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.MarkerOptions;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private double lat, longt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
lat = getIntent().getIntExtra("Lat",0);
longt = getIntent().getIntExtra("Long",0);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
/**
* Manipulates the map once available. This callback is triggered when the map is ready to be
* used. This is where we can add markers or lines, add listeners or move the camera. In this
* case, we just add a marker near Sydney, Australia. If Google Play services is not installed
* on the device, the user will be prompted to install it inside the SupportMapFragment. This
* method will only be triggered once the user has installed Google Play services and returned
* to the app.
*/
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(lat,longt);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
I also have added all permisions and api key to menifest
AndroidMenifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.googlemapdemo" >
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<permission
android:name="package.name.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-library android:name="com.google.android.maps" />
<uses-permission android:name="package.name.permission.MAPS_RECEIVE" />
<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" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!--
The API key for Google Maps-based APIs is defined as a string resource.
(See the file "res/values/google_maps_api.xml").
Note that the API key is linked to the encryption key used to sign the APK.
You need a different API key for each encryption key, including the release key that is used to
sign the APK for publishing.
You can define the keys for the debug and release targets in src/debug/ and src/release/.
-->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="#string/google_maps_key" />
<activity
android:name=".MapsActivity"
android:label="#string/title_activity_maps" >
</activity>
</application>
</manifest>
You need to add the package name and SHA certificate fingerprint on the developers console, under your key. Have you done that?
You need to add map key to show googlemap on your application.I didn't see it in the manifest file,i see only key for using GeoCoder
Try after adding your google map key. Example is shown below
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" />
Try this, it may help you:
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = null;
try {
addresses = geocoder.getFromLocationName("Location String", 1);
} catch (IOException e) {
e.printStackTrace();
}
Address address = addresses.get(0);
if(addresses.size() > 0) {
double latitude = addresses.get(0).getLatitude();
double longitude = addresses.get(0).getLongitude();
LatLng latLng = new LatLng(latitude, longitude);
mMap.addMarker(new MarkerOptions().position(latLng));
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(10));
}

Cannot resolve symbol 'mapView' on android studio

After tons of tries I couldn't found how to solve this.
Android won't resolve the symbol 'mapView' on:
mMapView = (MapView) v.findViewById(R.id.mapView);
What i'm trying to do is to get google maps to work on the emulator.
I know this code is working on another PC, so my guess here that there's something else.. (a system issue or google maps api problem)
here's the code.
Can anyone help? Thank you..
package com.example.matant.gpsportclient.Controllers;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.matant.gpsportclient.AsyncResponse;
import com.example.matant.gpsportclient.R;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
/**
* Created by matant on 8/24/2015.
*/
public class GoogleMapFragmentController extends Fragment implements AsyncResponse {
MapView mMapView;
private GoogleMap googleMap;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// inflat and return the layout
View v = inflater.inflate(R.layout.fragment_google_map_fragment_controller, container,
false);
mMapView = (MapView) v.findViewById(R.id.mapView);
mMapView.onCreate(savedInstanceState);
mMapView.onResume();// display map immediately
try {
MapsInitializer.initialize(getActivity().getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}
googleMap = mMapView.getMap();
// latitude and longitude
double latitude = 17.385044;
double longitude = 78.486671;
// create marker
MarkerOptions marker = new MarkerOptions().position(
new LatLng(latitude, longitude)).title("Hello Maps");
// Changing marker icon
marker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ROSE));
// adding marker
googleMap.addMarker(marker);
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(17.385044, 78.486671)).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
googleMap.getUiSettings().setZoomControlsEnabled(true);
// Perform any camera updates here
return v;
}
#Override
public void onResume() {
super.onResume();
mMapView.onResume();
}
#Override
public void onPause() {
super.onPause();
mMapView.onPause();
}
#Override
public void onDestroy() {
super.onDestroy();
mMapView.onDestroy();
}
#Override
public void onLowMemory() {
super.onLowMemory();
mMapView.onLowMemory();
}
#Override
public void handleResponse(String resStr) {
}
#Override
public void sendDataToDBController() {
}
#Override
public void preProcess() {
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.matant.gpsportclient"
android:versionCode="1"
android:versionName="1.0" >
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-sdk
android:minSdkVersion="17"
android:targetSdkVersion="17" />
<permission
android:name="com.example.matant.gpsportclient.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.MANAGE_DOCUMENTS" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<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-permission android:name="com.example.matant.gpsportclient.permission.MAPS_RECEIVE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".SplashScreen"
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=".Controllers.ForgotPassword"
android:label="#string/app_name" >
</activity>
<activity
android:name=".Controllers.Login"
android:label="#string/title_activity_login" >
</activity>
<activity
android:name=".Controllers.SignUp"
android:label="#string/title_activity_sign_up" >
</activity>
<activity
android:name=".MainScreen"
android:label="#string/title_activity_main_screen" >
</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="#string/google_maps_key" />
</application>
</manifest>
build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "23.0.0 "
defaultConfig {
applicationId "com.example.matant.gpsportclient"
minSdkVersion 23
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors {
}
}
dependencies {
compile files('C:/Users/Adi/AndroidStudioProjects/GPSportClient/libs/additionnal.jar')
compile files('C:/Users/Adi/AndroidStudioProjects/GPSportClient/libs/mail.jar')
compile files('C:/Users/Adi/AndroidStudioProjects/GPSportClient/libs/activation.jar')
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:22.2.1'
compile 'com.google.android.gms:play-services-maps:7.8.0'
}
Layout:
<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" >
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />
</RelativeLayout>
Replace
mMapView = (MapView) v.findViewById(R.id.mapView);
with
mMapView = (MapView) v.findViewById(R.id.map);
as the id of your fragment is map. See the xml
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />

Categories

Resources