why Can't I see the map

Shakeeb Ayaz

I want to run demo Google map but it is not showing map. Here is my code.I have checked API key it is correct

package com.example.googlemaps;
import com.google.android.maps.MapActivity;
import android.os.Bundle;
import android.view.Menu;

public class MainActivity extends MapActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return true;
    }

} 

Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.googlemaps"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8"/>
    <uses-permission android:name="android.permission.INTERNET" />

    <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.androidhive.googlemaps.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

layout xml

<?xml version="1.0" encoding="utf-8"?>
<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:apiKey="@string/Api_key"
/>
qulfille

This is old way for getting map. Google has updated it now. use

<fragment
            android:id="@+id/map"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            class="com.google.android.gms.maps.SupportMapFragment" />

in your xml layout.

provide your API key in manifest file as

<meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="YOUR-API-KEY" />

in your application tag.

also don't extend MapActivity. Instead extend normal Activity

then write below code in your OnCreate

int status = GooglePlayServicesUtil
                .isGooglePlayServicesAvailable(getBaseContext());

    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

        // Getting reference to the SupportMapFragment
        SupportMapFragment fragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);

        // Getting Google Map
        myMap = fragment.getMap();

        // Enabling MyLocation in Google Map
        myMap.setMyLocationEnabled(true);

        // Getting LocationManager object from System Service
        // LOCATION_SERVICE
        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

        // Creating a criteria object to retrieve provider
        Criteria criteria = new Criteria();

        // Getting the name of the best provider
        String provider = locationManager.getBestProvider(criteria, true);

        // Getting Current Location From GPS
        Location location = locationManager.getLastKnownLocation(provider);

        if (location != null) {
            onLocationChanged(location);
        }

        locationManager.requestLocationUpdates(provider, 20000, 0, this);
    }

also add following code on OnResume

int resultCode = GooglePlayServicesUtil
                .isGooglePlayServicesAvailable(getApplicationContext());

    if (resultCode == ConnectionResult.SUCCESS) {

    } else {
        GooglePlayServicesUtil.getErrorDialog(resultCode, this,
                RQS_GooglePlayServices);
    }

that's it. All the best!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related