Android Map View is not loading content - java

first off all I know here are many threads about this problem, but I read them all ,and try pretty much everything.
So what is my problem. I am developing an app with Google maps, and I also occur that well known problem that mapView is loaded fine, but it contains nothing (only grey blank rectangles).
Here is what I tried:
I tripplecheck my API code
I regenerate my API code
I check all the permmisions
And a lot of other stuff
main_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.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:enabled="true"
android:apiKey="my key"/>
</RelativeLayout>
manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gps.gpsclientsoftware"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<uses-library android:name="com.google.android.maps"/>
<activity
android:name="com.gps.gpsclientsoftware.GPSClientActivity"
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>
activity code:
package com.gps.gpsclientsoftware;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class GPSClientActivity extends MapActivity {
private MapView mapView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
One warning that I found when I launch my app, was:
03-11 17:51:03.751: E/MapActivity(8581): Couldn't get connection factory client
Hope you can help me.

Check this...
http://ucla.jamesyxu.com/?p=287
You need to override all the method. Don't miss any one.
I did it, and I got a working MapView.
(onLowMemory seems can be skipped.)
(MapView works better than MapFragment.)

Since 12/2012 Google released Google maps version 2.
This means that new applications should use this version and that api keys are provided only for v2 maps.
Your implementation seems to be for google maps v1.
Check here for a detailed guide, but from a quick look I see that the following are missing from your android manifest:
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="your_api_key"/>
<permission
android:name="your.project.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="your.project.permission.MAPS_RECEIVE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="your key" />
Also to run google maps on your phone you need to install google play services. In order to check that it is already installed and google maps v2 work on your device I suggested using an application that uses v2 maps like trulia.

As hsu.tw pointed out, we need to call lifecycle method of MapView from fragment lifecycle methods.
But having MapView part of layout file causes issue as onCreate called before inflating fragments layout [So MapView's onCreate() never be called].
Below approach works for me. [Might need to take care of removing MapView if you are adding this Fragment to BackStack]
class MapFragment : BaseFragment() {
var mapView: MapView? = null
override fun getFragmentLayout() = R.layout.fragment_map
override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
(view as? LinearLayout)?.addView(mapView, LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT))
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// create map view
mapView = MapView(context /*, GoogleMapOption for configuration*/)
mapView?.onCreate(savedInstanceState)
}
override fun onStart() {
super.onStart()
mapView?.onStart()
}
override fun onPause() {
super.onPause()
mapView?.onPause()
}
override fun onResume() {
super.onResume()
mapView?.onResume()
}
override fun onSaveInstanceState(outState: Bundle?) {
super.onSaveInstanceState(outState)
mapView?.onSaveInstanceState(outState)
}
override fun onStop() {
super.onStop()
mapView?.onStop()
}
override fun onLowMemory() {
super.onLowMemory()
mapView?.onLowMemory()
}
override fun onDestroy() {
super.onDestroy()
mapView?.onDestroy()
}
}

Replace "my key" in your manifest with your actual API key.
android:apiKey="my key"/>
An API key can be otained here:
https://developers.google.com/maps/documentation/android/start#obtaining_an_api_key

Related

Android webview can't onPause without the app crashing (Need to stop youtube playing in background)

I'm making this thread because the answers of similar threads didn't work in my case. I'm trying to publish a simple webview app and some of the pages have embedded youtube videos in them. They keep on playing after the screen is locked on Android 7.0 and below (seems to work fine on android 8) and this is a reason for the app to get rejected.
I've tried adding network state permission and also utilizing "onPause" as I have seen recommended in other similar threads. If I put in some code that uses onPause (regardless if I use on Resume afterwards or not), the app compiles and the moment it runs on my phone, it disappears a second later (I'm starting to think that maybe the app pauses upon running). Currently, my code is such that I've used onPause without it crashing but maybe not in a right way because the audio keeps on playing. I'm using just android studio, no frameworks.
I've replaced the actual URl with test.com for the purpose of posting here. Here is the code I'm using in MainActivity:
package com.test.moqtasvatba;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.google.firebase.appindexing.Action;
import com.google.firebase.appindexing.FirebaseAppIndex;
import com.google.firebase.appindexing.FirebaseUserActions;
import com.google.firebase.appindexing.Indexable;
import com.google.firebase.appindexing.builders.Actions;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.loadUrl("https://www.test.com");
webView.setWebViewClient(new WebViewClient());
// ATTENTION: This was auto-generated to handle app links.
Intent appLinkIntent = getIntent();
String appLinkAction = appLinkIntent.getAction();
Uri appLinkData = appLinkIntent.getData();
}
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
public Action getIndexApiAction() {
return Actions.newView("Main", "https://www.test.com");
}
#Override
public void onStart() {
super.onStart();
// ATTENTION: This was auto-generated to implement the App Indexing
API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
FirebaseAppIndex.getInstance().update(new
Indexable.Builder().setName("Main").setUrl("https://www.test.com").build());
FirebaseUserActions.getInstance().start(getIndexApiAction());
}
#Override
public void onStop() {
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
FirebaseUserActions.getInstance().end(getIndexApiAction());
super.onStop();
}
WebView webView; // Initialize this somewhere
#Override
protected void onPause(){
super.onPause();
if(webView != null){
webView.onPause();
webView.pauseTimers();
}
}
#Override
protected void onResume(){
super.onResume();
if(webView != null){
webView.onResume();
webView.resumeTimers();
}
}
}
Activity main 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"
tools:context=".MainActivity">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/webView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
/>
</android.support.constraint.ConstraintLayout>
And the AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.moqtasvatba">
<uses-permission android:name="android.permission.INTERNET"></uses-
permission>
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="26" />
<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=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:scheme="https"
android:host="www.test.com" />
</intent-filter>
</activity>
</application>
</manifest>
I would very much appreciate some advice on how to properly use onPause and where to put it exactly so that the app doesn't stop right after opening.

MapFragment won't load

I'm just trying to see a map in my android app and i'm having some troubles.
I've been following severals tutorial and still a have no result.
I downloaded google play services from my sdk manager and imported google_play_services_lib correctly i guess.
So i created a Google Maps API Key from Google Api Console using my packagename and my SHA1, which i found using cmd and also in eclipse in window>preferences>android>build, i got the same SHA1 in both ways so i think it's correct.
So i set my manifest like this
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.googlemapprova"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="21" />
<permission android:name="com.example.googlemapprova.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.example.googlemapprova.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" >
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="my api key" />
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<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>
in my layout file i created just a map fragment
<fragment
android:id="#+id/map"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignTop="#+id/textView1"
android:layout_centerHorizontal="true" />
I've changed android:name with class="com.google.android.gms.maps.SupportMapFragment" because of a tutorial
And then my main
package com.example.googlemapprova;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
public class MainActivity extends FragmentActivity {
GoogleMap googleMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
// Loading map
initilizeMap();
initializeUiSettings();
initializeMapLocationSettings();
initializeMapTraffic();
initializeMapType();
initializeMapViewSettings();
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
protected void onResume() {
super.onResume();
// initilizeMap();
}
private void initilizeMap() {
googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getApplicationContext(), "Sorry! unable to create maps", Toast.LENGTH_SHORT).show();
}
}
public void initializeUiSettings() {
googleMap.getUiSettings().setCompassEnabled(true);
googleMap.getUiSettings().setRotateGesturesEnabled(false);
googleMap.getUiSettings().setTiltGesturesEnabled(true);
googleMap.getUiSettings().setZoomControlsEnabled(true);
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
}
public void initializeMapLocationSettings() {
googleMap.setMyLocationEnabled(true);
}
public void initializeMapTraffic() {
googleMap.setTrafficEnabled(true);
}
public void initializeMapType() {
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
public void initializeMapViewSettings() {
googleMap.setIndoorEnabled(true);
googleMap.setBuildingsEnabled(false);
}
}
i got the code in my main from a tutorial and modified it just a little, in this way i managed my app to not crash(if i don't use this code my app will crash).
this is what i get
i'm using a motorola moto g3 phone to try it
So i tryed to download aLogcat on my phone from play store, but i get nothing on it, maybe i'm using it in the wrong way.
this is the tutorial i used the most
Can someone help me to view a map on my app? I can't understand why it's not working
I'm trying to get the Logcat now
Your map fragment is loaded, but map content does not.
Where you placed your API key?
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="my api key" />

Here maps doesn't show in real device

I try to make a simple app with here map in Android studio. But it doesn't display in device. I downloaded heremap api and add it's jar to my project. Can you help me please?
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.lojika.helloheremaps" >
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.lojika.helloheremaps.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.here.android.maps.appid"
android:value="XXXXXXXXXXXXXXXXXXXXXX"/>
<meta-data android:name="com.here.android.maps.apptoken"
android:value="XXXXXXXXXXXXXXXXXXXXXX"/>
<service
android:name="com.here.android.mpa.service.MapService"
android:label="HereMapService"
android:process="global.Here.Map.Service.v2"
android:exported="true" >
<intent-filter>
<action android:name="com.here.android.mpa.service.MapService" >
</action>
</intent-filter>
</service>
</application>
</manifest>
MainActivity.java
package com.example.lojika.helloheremaps;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import com.here.android.mpa.common.MapActivity;
import com.here.android.mpa.common.GeoCoordinate;
import com.here.android.mpa.common.OnEngineInitListener;
import com.here.android.mpa.mapping.Map;
import com.here.android.mpa.mapping.MapFragment;
public class MainActivity extends Activity {
// map embedded in the map fragment
private Map map = null;
// map fragment embedded in this activity
private MapFragment mapFragment = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Search for the map fragment to finish setup by calling init().
mapFragment = (MapFragment) getFragmentManager().findFragmentById( R.id.mapfragment);
mapFragment.init(new OnEngineInitListener() {
#Override
public void onEngineInitializationCompleted(OnEngineInitListener.Error error) {
if (error == OnEngineInitListener.Error.NONE) {
// retrieve a reference of the map from the map fragment
onMapFragmentInitializationCompleted();
// map = mapFragment.getMap();
// Set the map center to the Vancouver region (no animation)
//map.setCenter(new GeoCoordinate(15.1447, 120.5957, 0.0), Map.Animation.NONE);
// Set the zoom level to the average between min and max
//map.setZoomLevel(
// (map.getMaxZoomLevel() + map.getMinZoomLevel()) / 2);
} else {
System.out.println("ERROR: Cannot initialize Map Fragment");
}
}
});
}
private void onMapFragmentInitializationCompleted() {
// retrieve a reference of the map from the map fragment
map = mapFragment.getMap();
// Set the map center coordinate to the Vancouver region (no animation)
map.setCenter(new GeoCoordinate(49.196261, -123.004773, 0.0),
Map.Animation.NONE);
// Set the map zoom level to the average between min and max (no
// animation)
map.setZoomLevel((map.getMaxZoomLevel() + map.getMinZoomLevel()) / 2);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
}
activity_main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/hello_world"
tools:context=".MainActivity" />
<!-- Map Fragment embedded with the map object -->
<fragment
class="com.here.android.mpa.mapping.MapFragment"
android:id="#+id/mapfragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
I think you did not added all permission required, Please check below image
I know that the answer is very late. But I was looking for a solution to the same problem. And maybe it will help someone.
Map Service is an Android service that facilitates the use of a shared disk cache among applications that use the HERE SDK. This service must be embedded and deployed with your HERE-enabled application; otherwise, the MISSING_SERVICE error code is returned via the onEngineInitializationCompleted() callback.
To embed Map Service, add the following lines inside the <application> </application> section in your
AndroidManifest.xml file:
<service
android:name="com.here.android.mpa.service.MapService"
android:label="HereMapService"
android:process="global.Here.Map.Service.v2"
android:exported="true" >
<intent-filter>
<action android:name="com.here.android.mpa.service.MapService" >
</action>
</intent-filter>
</service>

Can't access google servers through android app

I'm developing a android app cappable of acessing the maps API, but i'm stuck on error:
Google Maps Android APIļ¹• Failed to load map. Error contacting Google
servers. This is probably an authentication issue (but could be due to
network errors).
Already did the key and activate everithing in google API console.
These are my files:
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="hflopes.mapas" >
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"></uses-permission>
<uses-feature android:glEsVersion="0x00020000"
android:required="true"></uses-feature>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<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="AIzaSyBTObQnY1mxYwXw1g8txH5ApBu8ovxv730" />
<activity android:name=".MostraAlunosProximos"></activity>
<activity
android:name=".mapas"
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>
mapas_layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/mapa"/>
</LinearLayout>
Class for Loading the map:
package hflopes.mapas;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.AttributeSet;
import android.view.View;
/**
* Created by HFLopes on 24/07/2014.
*/
public class MostraAlunosProximos extends FragmentActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mapa_layout);
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.mapa, new MapaFragment());
transaction.commit();
}
}
Any Idea?
Thanks for the help
I have done the same.
Try this in your MostraAlunosProximos class oncreate,
int status = GooglePlayServicesUtil
.isGooglePlayServicesAvailable(getBaseContext());
// Showing status
if (status != ConnectionResult.SUCCESS) { // Google Play Services
// are
// not available
int requestCode = 10;
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status,
this, requestCode);
dialog.show();
} else { // Google Play Services are available
SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.mapa);
// Getting GoogleMap object from the fragment
if (fm != null) {
googleMap = fm.getMap();
}
// Enabling MyLocation Layer of Google Map
googleMap.setMyLocationEnabled(true);
}
And add this line android:name="com.google.android.gms.maps.SupportMapFragment" inside fragment of your xml file.
And also i see there are two metadeta for google play service remove that.
Hope this helps
I did this and got to work on my device. Try it.
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="br.com.example.mapsexample"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:maxSdkVersion="20"
android:minSdkVersion="17"
android:targetSdkVersion="17" />
<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="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<!-- 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="br.com.example.mapsexample.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>
<!-- Goolge API Key -->
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="your_api_key_here" />
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
</application>
</manifest>
Class for loading map
package br.com.example.mapsexample;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
public class MainActivity extends Activity {
GoogleMap mGoogleMap;
#Override
protected void onCreate( final Bundle savedInstanceState ) {
super.onCreate( savedInstanceState );
this.setContentView( R.layout.activity_main );
try {
this.initializeMap();
} catch ( final Exception e ) {
// TODO: handle exception
}
}
#Override
protected void onResume() {
super.onResume();
this.initializeMap();
}
private void initializeMap() {
if ( this.mGoogleMap == null ) {
this.mGoogleMap = ( ( MapFragment ) this.getFragmentManager().findFragmentById( R.id.mapFragment ) ).getMap();
if ( this.mGoogleMap == null ) {
Toast.makeText( this.getApplicationContext(), "Is not possible load the map", Toast.LENGTH_SHORT ).show();
}
}
}
}
Layout Maps
<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/mapFragment"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
Solved it!
it was missing this on build.graddle
signingConfigs {
debug {
storeFile file("debug.jks")
}
}
Thanks for the help

I Have Error a google map android api v2 is not loaded

I have a project of google map android api v2 but i run this project in my phone a logcat has error
Falied to load map.Error contacting Google servers. This is probable an authentication issue(but could be due to network errors)
so my code is Here
in class HomeActivity
package com.mpa.emvi;
import com.mpa.emvi.R;
import android.support.v4.app.FragmentActivity;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import android.os.Bundle;
//import android.app.Activity;
import android.view.Menu;
public class HomeActivity extends FragmentActivity {
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
setUpMapIfNeeded();
}
#Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home, menu);
return true;
}
}
this is a Layout HomeActivity
<?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:id="#+id/send"
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=".HomeActivity" >
<fragment 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"
android:layout_centerHorizontal="true"/>
<Button
android:id="#+id/sendDS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/map"
android:layout_centerHorizontal="true"
android:text="#string/send_destination"/>
</RelativeLayout>
And this is a Manifest File
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mpa.emvi"
android:versionCode="2"
android:versionName="2.1.0">
<permission
android:name="com.mpa.emvi.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.mpa.emvi.permission.MAPS_RECEIVE"/>
<!-- Copied from Google Maps Library/AndroidManifest.xml. -->
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="14"/>
<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"/>
<!-- External storage for caching. -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<!-- My Location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<!-- Maps API needs OpenGL ES 2.0. -->
<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.mpa.emvi.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.maps.v2.API_KEY"
android:value="My-Release-API-KEY"/>
</application>
</manifest>
You see my code. What do you thing?
Ok I'm try to seach same issue in this and i try re-generate new api key but not work.
In my project aleady import a google service lib and android support v4.
In google console i open google map android api v2 service.
In google console android apps: my-SHA1-serials;com.mpa.emvi
OK a lasting I'm a newbie android programmer and sorry for my english is poor.
Thank for every Answer.
Have you added your Api key in Manifest File?
Ok now my app is showing the map.I forget delete old apk file and re-install new apk file it work.Thank For Any Answer Thank.

Categories

Resources