2013-07-23 2 views
0

Google지도 서비스를 콘솔에 올려 놓고 디버그 키를 생성합니다.프로그래밍 방식으로 추가 된 Google지도 빈 화면

나는 그것을 디버그했습니다. mMap과 mLocation 모두 null이 아닙니다. 나는 위도와 경도를 성공적으로 얻을 수있다.

하지만지도는 여전히 비어 있습니다.

도와주세요.

<?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> 

자바 코드 :

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() { 


    // Do a null check to confirm that we have not already instantiated the map. 
    if (mMap == null) { 
    // Try to obtain the map from the MapFragment. 
    mMap = mMapView.getMap(); 
    // Check if we were successful in obtaining the 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)); 
    // It isn't possible to set a fragment's id programmatically so we set a tag instead and 
    // search for it using that. 

    mMapView = (SupportMapFragment) getSupportFragmentManager() 
      .findFragmentByTag(MAP_FRAGMENT_TAG); 

    // We only create a fragment if it doesn't already exist. 
    if (mMapView == null) { 
     // To programmatically add the map, we first create a SupportMapFragment. 
     mMapView = SupportMapFragment.newInstance(); 

     // Then we add it using a 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 we were successful in obtaining the 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(); 
} 
012,351,641 여기
<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> 

매니페스트 XML입니다 :

샘 여기

는 레이아웃 XML입니다

+0

LogCat에 무엇을 말합니까? – Raptor

+0

@ShivanRaptor LogCat에는 오류가 없습니다. 모든게 잘된 것 같아.하지만지도는 없어. 문제없이 여행하여 내 위치를 찾을 수 있습니다. 내가 프로젝트를 디버그하면지도와 위치가 null이 아닙니다! – sam

+0

패키지 이름과 Keystore –

답변

0

먼저 올바른 디버그 키를 사용하고 있는지 확인하십시오. 두 번째 사용 허가. manifest.xml에서 반복되는 권한을 제거하십시오. 대신 간단한 조각

mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapView)).getMap(); 

<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" /> 

사용 supportmap 조각

이 시도 ... 당신이 http://mobisys.in/blog/2012/12/google-rolls-out-android-maps-api-v2/

코드가 명확하지 않다 유용 찾을 수 있습니다 간단한 튜토리얼을 따라 당신이지도에 잠깐 동안을 보여줄 때. 귀하의 코드에서 이것을 찾을 수 없습니다.

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 i = mMap.getCameraPosition();  
    Log.d("MAP"," after setting zoom 2 :"+i.zoom); 
    mMap.moveCamera(center); 
+0

디버그 키가 올바른 키입니다. required = "false"를 추가하면지도를 표시 할 수 있지만 위치를 찾을 수 없습니다. mLocationClient.connect()가 작동하지 않습니다. 코드를주의 깊게 읽으면 다음과 같이이 SupportMapFragment를 동적으로 추가 한 것을 볼 수 있습니다. mMapView = (SupportMapFragment) getSupportFragmentManager(). findFragmentByTag (MAP_FRAGMENT_TAG);
if (mMapView == null) { mMapView = SupportMapFragment.newInstance(); ... – sam

+0

코드가 분명하지 않습니다. – CodingRat

0

아직도 무슨 일이 일어 났는지는 알 수 없습니다. 그러나, 여기에 내가 찍은 단계 :

  1. 내가 Gaurav 샤르마하여 조언을했다가, 권한이 트리거 할 수없는 onConnected()를 발생
  2. = "false"를 요구합니다.
  3. 그렇다면 required = "true"권한을 변경했습니다. 지도가 마술처럼 보인다! 이 얼마나 놀라운 Google지도!

어쨌든지도가 표시됩니다. 무엇이 잘못되었는지 모릅니다. 감사합니다. @GauravSharma

관련 문제