Вопрос

I am trying to run a simple google map but its not working its showing only map background.checked on both emulator and device.

xml file:-

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment"/>

Java class

import android.os.Bundle; import
com.google.android.gms.maps.GoogleMap; import
com.google.android.gms.maps.SupportMapFragment; import
com.google.android.gms.maps.model.BitmapDescriptorFactory; import
com.google.android.gms.maps.model.LatLng; import
com.google.android.gms.maps.model.Marker; import
com.google.android.gms.maps.model.MarkerOptions;

public class LocationGB extends android.support.v4.app.FragmentActivity {     
    static final LatLng HAMBURG = new LatLng(53.558, 9.927); 
    static final LatLng KIEL = new LatLng(53.551, 9.993);
    private GoogleMap map;
     @Override protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location);
        map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(android.R.id.content)).getMap();

        if (map!=null){
            Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
                .title("Hamburg"));
            Marker kiel = map.addMarker(new MarkerOptions()
                .position(KIEL)
                .title("Kiel")
                .snippet("Kiel is cool")
                .icon(BitmapDescriptorFactory
                .fromResource(R.drawable.ic_launcher))); 
        }
    } 
}
Это было полезно?

Решение

In your manifest file you have to add:

<permission
  android:name="your.application.package.permission.MAPS_RECEIVE"
  android:protectionLevel="signature" />

Like from: your.application.package to: com.themontcalm.droid

And yes if you are using

<uses-library android:name="com.google.android.maps" />

then remove from manifest file:

and add your api code to in manifest file also:

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

And in manifest file you are missing this:

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

so also add to this also in manifest file:

you should change android.R.id.content with R.id.map in your code

Something like:

map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

Другие советы

In order to use google Map Service,

Please visit code.google.com and get you ApiKey.

create a project there->go to services->Activate the Google Maps Android API v2. Then Create Key for your application.

Add the following permissions to your manifest.

 <uses-permission android:name="com.anchit.locationapi.maps.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" />

use

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

also add

<permission
        android:name="com.anchit.locationapi.maps.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

inside your Application Tag in manifest.

Use

map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
        .getMap();

in your code.

and set the other required properties. Now Run the Application.

In your Manifest add

<permission
  android:name="your.application.package.permission.MAPS_RECEIVE"
  android:protectionLevel="signature" />

Also add to your application tag

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

You can also visit Google Map V2 , this and Example of map v2

Hope it helps you.

Edit:

As per you said Add this xml file in your project

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

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

And access it with in your map activity

 map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
        .getMap(); 
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top