android - google map blank screen, programmatic added -
i have put google map service on console, , generate debug key.
i've debug it, mmap , mlocation both not null. can latitude , longitude successfully.
but map still blank.
please help.
sam
here layout xml:
<relativelayout 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:background="#f2f5fa" tools:context=".locfm" > <!-- top bar component --> <imageview android:id="@+id/loc_topbar" android:layout_width="match_parent" android:layout_height="60dp" android:layout_alignparenttop="true" android:layout_alignparentleft="true" android:background="@drawable/topbar" /> <textview android:id="@+id/loc_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbottom="@+id/loc_backbtn" android:layout_centerhorizontal="true" android:text="@string/loc_title" android:textcolor="#171717" android:textsize="@dimen/title_font" /> <imageview android:id="@+id/loc_backbtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_marginleft="10dp" android:layout_margintop="10dp" android:src="@drawable/backbtn" /> <!-- body --> <framelayout android:id="@+id/map_fragment" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_below="@+id/loc_topbar" android:layout_above="@+id/loc_bottombar" > <!-- put fragments dynamically --> </framelayout> <!-- bottom bar component --> <imageview android:id="@+id/loc_bottombar" android:layout_width="match_parent" android:layout_height="60dp" android:layout_alignparentbottom="true" android:layout_alignparentleft="true" android:background="@drawable/bottombar" /> <button android:id="@+id/locbtn" android:layout_width="@dimen/halfbutton_width" android:layout_height="@dimen/halfbutton_height" android:layout_aligntop="@+id/loc_bottombar" android:layout_alignparentleft="true" android:layout_margintop="@dimen/halfbutton_margintop" android:background="@drawable/halfbutton_bg" android:text="@string/button_loc" android:textsize="@dimen/button_font"/> <button android:id="@+id/loc_nextbtn" android:layout_width="@dimen/halfbutton_width" android:layout_height="@dimen/halfbutton_height" android:layout_aligntop="@+id/loc_bottombar" android:layout_alignparentright="true" android:layout_margintop="@dimen/halfbutton_margintop" android:background="@drawable/halfbutton_bg" android:text="@string/button_continue" android:textsize="@dimen/button_font"/> <imageview android:id="@+id/nextimg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignright="@+id/loc_nextbtn" android:layout_aligntop="@+id/loc_nextbtn" android:layout_marginright="@dimen/buttonicon_marginright" android:layout_margintop="@dimen/buttonicon_margintop" android:src="@drawable/next" /> <imageview android:id="@+id/locimg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignright="@+id/locbtn" android:layout_aligntop="@+id/locbtn" android:layout_marginright="@dimen/buttonicon_marginright" android:layout_margintop="@dimen/buttonicon_margintop" android:src="@drawable/location" /> </relativelayout>
here manifest xml:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.map" android:versioncode="1" android:versionname="1.0" > <uses-sdk android:minsdkversion="7" 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_coarse_location" /> <uses-permission android:name="android.permission.call_phone" /> <uses-permission android:name="android.permission.receive_boot_completed" /> <uses-permission android:name="android.permission.access_coarse_location" > </uses-permission> <uses-permission android:name="android.permission.access_fine_location" > </uses-permission> <uses-permission android:name="android.permission.internet" /> <uses-permission android:name="android.permission.access_network_state" > </uses-permission> <uses-permission android:name="android.permission.restart_packages" /> <uses-permission android:name="android.permission.read_phone_state" /> <uses-permission android:name="android.permission.wake_lock" /> <uses-permission android:name="android.permission.write_external_storage" /> <uses-permission android:name="android.permission.change_wifi_state" /> <uses-permission android:name="android.permission.access_wifi_state" /> <uses-permission android:name="com.google.android.providers.gsf.permission.read_gservices"/> <permission android:name="com.example.map.permission.maps_receive" android:protectionlevel="signature" /> <uses-permission android:name="com.example.map.permission.maps_receive" /> <uses-feature android:glesversion="0x00020000" android:required="true" /> <application android:allowbackup="true" android:icon="@drawable/logo_icon" 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" /> <activity android:name="com.example.map.locfm" android:configchanges="orientation|keyboardhidden" android:label="@string/app_name" android:nohistory="true" android:screenorientation="portrait" android:theme="@android:style/theme.notitlebar.fullscreen" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> </application>
java code:
package com.example.map; import java.text.simpledateformat; import java.util.arraylist; import java.util.date; import java.util.list; import java.util.timer; import java.util.timertask; import android.app.alertdialog; import android.app.progressdialog; import android.app.alertdialog.builder; import android.content.dialoginterface; import android.location.criteria; import android.location.location; import android.location.locationmanager; import android.os.bundle; import android.os.handler; import android.os.message; import android.support.v4.app.fragmentactivity; import android.support.v4.app.fragmenttransaction; import android.support.v4.app.fragmentmanager; import android.view.view; import android.view.windowmanager; import android.view.view.onclicklistener; import android.widget.adapterview; import android.widget.arrayadapter; import android.widget.button; import android.widget.edittext; import android.widget.imageview; import android.widget.radiobutton; import android.widget.radiogroup; import android.widget.spinner; import android.widget.textview; import android.widget.toast; import android.widget.adapterview.onitemselectedlistener; import com.google.android.gms.common.connectionresult; import com.google.android.gms.common.googleplayservicesclient.connectioncallbacks; import com.google.android.gms.common.googleplayservicesclient.onconnectionfailedlistener; import com.google.android.gms.location.locationclient; import com.google.android.gms.location.locationlistener; import com.google.android.gms.location.locationrequest; import com.google.android.gms.maps.cameraupdatefactory; import com.google.android.gms.maps.googlemap; import com.google.android.gms.maps.supportmapfragment; import com.google.android.gms.maps.uisettings; import com.google.android.gms.maps.model.latlng; import com.google.android.gms.maps.model.markeroptions; public class locfm extends fragmentactivity implements onclicklistener, connectioncallbacks, onconnectionfailedlistener, locationlistener { timer positiontimer; private googlemap mmap; private location mlocation; private uisettings muisettings; private locationclient mlocationclient; private static supportmapfragment mmapview; private static final string map_fragment_tag = "map"; public fragmentmanager fmanager; double latitude, longitude, radius; string slat,slong,srange; boolean bmaploaded=false; int index =0; int positioncount=0; final int position_count_max=5; final int position_min=100; final int pssition_max_times=10; onlinehistoryinfo onlinehistory; progressdialog dialog; private static final locationrequest request = locationrequest.create() .setinterval(5000) // 5 seconds .setfastestinterval(16) // 16ms = 60fps .setpriority(locationrequest.priority_high_accuracy); protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setlocfm(); } @override public void onlocationchanged(location location) { if (mlocationclient != null && mlocationclient.isconnected()) { mlocation=mlocationclient.getlastlocation(); } if (mlocation!=null) { latitude= mlocation.getlatitude(); longitude= mlocation.getlongitude(); radius=mlocation.getaccuracy(); slat=double.tostring(latitude); slong=double.tostring(longitude); srange=double.tostring(radius); latlng latlng = new latlng(latitude, longitude); mmap.movecamera(cameraupdatefactory.newlatlng(latlng)); mmap.animatecamera(cameraupdatefactory.zoomto(15)); mmap.addmarker(new markeroptions().position(latlng).title(getstring(r.string.map_msg))); } positioncount++; if(mlocationclient==null || radius<=position_min) { mlocationclient.disconnect(); positioncount=0; button loc_nextbtn=(button)findviewbyid(r.id.loc_nextbtn); loc_nextbtn.setonclicklistener(this); loc_nextbtn.setvisibility(view.visible); imageview nextimg=(imageview)findviewbyid(r.id.nextimg); nextimg.setvisibility(view.visible); showdialog(false,null); } if(positioncount>=position_count_max) { mlocationclient.disconnect(); positioncount=0; } } @override public void onconnectionfailed(connectionresult result) { // todo auto-generated method stub } @override public void onconnected(bundle connectionhint) { setupmapifneeded(); position(); } @override public void ondisconnected() { // todo auto-generated method stub } private void mapdestroy() { if (mlocationclient != null) mlocationclient.disconnect(); if(mmapview!=null){ fragmenttransaction ft = mmapview.getactivity().getsupportfragmentmanager().begintransaction(); ft.remove(mmapview); ft.commit(); } if(mmap!=null) { mmap=null; } bmaploaded=false; } private void setupmapifneeded() { // null check confirm have not instantiated map. if (mmap == null) { // try obtain map mapfragment. mmap = mmapview.getmap(); // check if successful in obtaining map. if (mmap != null) { mmap.setmylocationenabled(true); } } } private void setuplocationclientifneeded() { if (mlocationclient == null) { mlocationclient = new locationclient( this, this, // connectioncallbacks this); // onconnectionfailedlistener } } @override public void onclick(view v) { switch (v.getid()) { case r.id.loc_backbtn: mapdestroy(); locfm.this.finish(); break; case r.id.loc_nextbtn: positioncount=0; mapdestroy(); break; case r.id.locbtn: relocate(); break; } } public void setlocfm(){ setcontentview(r.layout.loc_fm); loadmapfragment(); imageview loc_backbtn=(imageview)findviewbyid(r.id.loc_backbtn); loc_backbtn.setonclicklistener(this); button loc_nextbtn=(button)findviewbyid(r.id.loc_nextbtn); loc_nextbtn.setvisibility(view.gone); imageview nextimg=(imageview)findviewbyid(r.id.nextimg); nextimg.setvisibility(view.gone); button locbtn=(button)findviewbyid(r.id.locbtn); locbtn.setonclicklistener(this); bmaploaded=true; } private void loadmapfragment() { showdialog(true,getstring(r.string.alert_postion)); // isn't possible set fragment's id programmatically set tag instead , // search using that. mmapview = (supportmapfragment) getsupportfragmentmanager() .findfragmentbytag(map_fragment_tag); // create fragment if doesn't exist. if (mmapview == null) { // programmatically add map, first create supportmapfragment. mmapview = supportmapfragment.newinstance(); // add using fragmenttransaction. fragmenttransaction fragmenttransaction = getsupportfragmentmanager().begintransaction(); fragmenttransaction.add(r.id.map_fragment, mmapview, map_fragment_tag); fragmenttransaction.commit(); } setuplocationclientifneeded(); if (!mlocationclient.isconnected()) mlocationclient.connect(); } private void position() { showdialog(true,getstring(r.string.alert_postion)); if (mmap != null) { mmap.setmaptype(googlemap.map_type_normal); mmap.setmylocationenabled(true); locationmanager locationmanager = (locationmanager) getsystemservice(location_service); criteria criteria = new criteria(); criteria.setaccuracy(criteria.accuracy_fine); criteria.setaltituderequired(false); criteria.setbearingrequired(false); criteria.setcostallowed(true); criteria.setpowerrequirement(criteria.power_low); string provider = locationmanager.getbestprovider(criteria, true); mlocation = locationmanager.getlastknownlocation(provider); muisettings = mmap.getuisettings(); muisettings.setcompassenabled(true); muisettings.setmylocationbuttonenabled(false); // check if successful in obtaining map. mlocationclient.requestlocationupdates(request,this); // locationlistener } } public void relocate() { if (!mlocationclient.isconnected()) mlocationclient.connect(); wswlog.i("relocate()"); } /** * @param dialgshow */ public void showdialog(boolean dialgshow,final string message) { wswlog.i(" showdialog ("+dialgshow+","+message+")"); if(dialgshow) { if(dialog==null) { dialog = new progressdialog(this); dialog.setcancelable(false);//false { dialog.setmessage(message); } } else { dialog.setmessage(message); } dialog.show(); // if(null != positiontimer) { positiontimer.cancel(); } positiontimer= new timer(true); timertask task = new timertask(){ public void run() { handler.sendemptymessage(1); } }; positiontimer.schedule(task,pssition_max_times*1000, pssition_max_times*1000); } else { if(null != positiontimer) { positiontimer.cancel(); } if(dialog!=null) { dialog.dismiss(); } } } private handler handler = new handler() { @override public void handlemessage(message msg) { if(null != positiontimer) { positiontimer.cancel(); } showdialog(false,null); // alertnoposition(); super.handlemessage(msg); } }; /** */ private void alertnoposition() { alertdialog.builder builder = new builder(this); builder.setmessage(getstring(r.string.loc_alert_realloc)); builder.settitle(getstring(r.string.button_loc)); builder.setnegativebutton(getstring(r.string.button_cancel), new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { dialog.dismiss(); locfm.this.finish(); } }); builder.setpositivebutton(getstring(r.string.button_ok), new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { dialog.dismiss(); relocate(); } }); builder.create().show(); }
}
first check debug key whether using right one? second use following permission. remove repeated permission manifest.xml.
<permission android:name="com.example.map.permission.maps_receive" android:protectionlevel="signature" /> <uses-permission android:name="com.example.map.permission.maps_receive" android:required="false" /> <uses-permission android:name="com.google.android.providers.gsf.permission.read_gservices" android:required="false" />
use supportmap fragment instead of simple fragment
mmap = ((supportmapfragment) getsupportfragmentmanager().findfragmentbyid(r.id.mapview)).getmap();
follow simple tutorial may find usefull http://mobisys.in/blog/2012/12/google-rolls-out-android-maps-api-v2/
your code not clear...
try when showing lat-long on map. not find in code
mcurrentlattitude = mgpslistener.getlatituderaw(); mcurrentlongitude = mgpslistener.getlongituderaw(); latlng coordinates = new latlng(mcurrentlattitude, mcurrentlongitude); log.d("map"," setcurrentlocation lat-lang values: "+mcurrentlattitude +" : "+mcurrentlongitude); cameraupdate center= cameraupdatefactory.newlatlng(coordinates); string lpreviouszoomlevelstring = config.getsetting(getapplicationcontext(), "mapzoomlevel"); mpreviouszoomlevel = float.parsefloat(lpreviouszoomlevelstring); cameraupdate zoom=cameraupdatefactory.zoomto(mpreviouszoomlevel); mmap.animatecamera(zoom); cameraposition = mmap.getcameraposition(); log.d("map"," after setting zoom 2 :"+i.zoom); mmap.movecamera(center);
Comments
Post a Comment