Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
493 views
in Technique[技术] by (71.8m points)

android - google map not working on both emulator and device

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))); 
        }
    } 
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...